Skip to content

Commit 90eeaad

Browse files
committed
implement Window.zoom and event handler
Fix nwjs#372
1 parent 7045edc commit 90eeaad

6 files changed

Lines changed: 69 additions & 0 deletions

File tree

src/api/dispatcher.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,26 @@ void Dispatcher::OnEvent(int object_id,
8585
node::MakeCallback(objects_registry, "handleEvent", 3, argv);
8686
}
8787

88+
void Dispatcher::ZoomLevelChanged() {
89+
WebKit::WebView* web_view = render_view()->GetWebView();
90+
float zoom_level = web_view->zoomLevel();
91+
v8::Handle<v8::Value> v8win = web_view->mainFrame()->
92+
mainWorldScriptContext()->Global();
93+
v8::Handle<v8::Value> val = v8win->ToObject()->Get(v8::String::New("__nwWindowId"));
94+
95+
if (val->IsNull() || val->IsUndefined())
96+
return;
97+
98+
v8::Handle<v8::Value> registry =
99+
node::g_context->Global()->Get(v8::String::New("__nwObjectsRegistry"));
100+
if (registry->IsNull() || registry->IsUndefined())
101+
return;
102+
v8::Handle<v8::Object> objects_registry = registry->ToObject();
103+
104+
v8::Local<v8::Array> args = v8::Array::New();
105+
args->Set(0, v8::Number::New(zoom_level));
106+
v8::Handle<v8::Value> argv[] = {val, v8::String::New("zoom"), args };
107+
108+
node::MakeCallback(objects_registry, "handleEvent", 3, argv);
109+
}
88110
} // namespace api

src/api/dispatcher.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Dispatcher : public content::RenderViewObserver {
4343
// RenderViewObserver implementation.
4444
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
4545
virtual void DraggableRegionsChanged(WebKit::WebFrame* frame) OVERRIDE;
46+
virtual void ZoomLevelChanged() OVERRIDE;
4647

4748
void OnEvent(int object_id,
4849
std::string event,

src/api/window_bindings.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,39 @@ WindowBindings::CallObjectMethod(const v8::Arguments& args) {
8282
// static
8383
v8::Handle<v8::Value>
8484
WindowBindings::CallObjectMethodSync(const v8::Arguments& args) {
85+
v8::HandleScope scope;
86+
8587
v8::Local<v8::Object> self = args[0]->ToObject();
8688
int routing_id = self->Get(v8::String::New("routing_id"))->Int32Value();
8789
int object_id = self->Get(v8::String::New("id"))->Int32Value();
8890
std::string method = *v8::String::Utf8Value(args[1]);
8991

92+
if (method == "GetZoomLevel") {
93+
content::RenderViewImpl* render_view = static_cast<content::RenderViewImpl*>(
94+
content::ChildThread::current()->ResolveRoute(routing_id));
95+
if (!render_view)
96+
return v8::ThrowException(v8::Exception::Error(v8::String::New(
97+
"Unable to get render view in GetZoomLevel")));
98+
99+
float zoom_level = render_view->GetWebView()->zoomLevel();
100+
101+
v8::Local<v8::Array> array = v8::Array::New();
102+
array->Set(0, v8::Number::New(zoom_level));
103+
return scope.Close(array);
104+
}
105+
106+
if (method == "SetZoomLevel") {
107+
content::RenderViewImpl* render_view = static_cast<content::RenderViewImpl*>(
108+
content::ChildThread::current()->ResolveRoute(routing_id));
109+
if (!render_view)
110+
return v8::ThrowException(v8::Exception::Error(v8::String::New(
111+
"Unable to get render view in SetZoomLevel")));
112+
113+
double zoom_level = args[2]->ToNumber()->Value();
114+
render_view->OnSetZoomLevel(zoom_level);
115+
return v8::Undefined();
116+
}
117+
90118
return remote::CallObjectMethodSync(
91119
routing_id, object_id, "Window", method, args[2]);
92120
}

src/api/window_bindings.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ Window.prototype.__defineGetter__('title', function() {
133133
return this.window.document.title;
134134
});
135135

136+
Window.prototype.__defineSetter__('zoomLevel', function(level) {
137+
CallObjectMethodSync(this, 'SetZoomLevel', level);
138+
});
139+
140+
Window.prototype.__defineGetter__('zoomLevel', function() {
141+
return CallObjectMethodSync(this, 'GetZoomLevel', [])[0];
142+
});
143+
136144
Window.prototype.__defineSetter__('menu', function(menu) {
137145
if (v8_util.getConstructorName(menu) != 'Menu')
138146
throw new String("'menu' property requries a valid Menu");

tests/window/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
<button onclick="win.setAlwaysOnTop(true)">setAlwaysOnTop(true)</button>
7979
<button onclick="win.setAlwaysOnTop(false)">(false)</button>
8080
<br/>
81+
<button onclick="win.zoomLevel = 2">set zoomLevel to 2</button>
82+
<br/>
83+
<button onclick="win.zoomLevel = 0">set zoomLevel to 0</button>
8184
<br/>
8285
Reload the window and do all tests again.
8386
<button onclick="win.reload()">Reload</button>

tests/window/popup.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,17 @@
5050
console.log('Window is unmaximized');
5151
});
5252

53+
win.on('zoom', function(level) {
54+
document.getElementById('zoom').innerText = level;
55+
});
56+
5357
window.onload = function() {
5458
gui.Window.get().show();
5559
}
60+
document.write("zoomLevel = <div id='zoom'>" + win.zoomLevel + '</div>');
5661
</script>
62+
<br/>
63+
5764
<button onclick="javascript:enable = true;">Enable to be closed</button>
5865
<br/>
5966
<button onclick="win.leaveFullscreen()">Leave fullscreen</button>

0 commit comments

Comments
 (0)