Merge pull request #406 from atom/webview

Provide ways to set page's zoom level
This commit is contained in:
Cheng Zhao 2014-06-17 16:14:22 +08:00
commit 74fe964f75
14 changed files with 202 additions and 1 deletions

View file

@ -41,6 +41,7 @@
'atom/renderer/lib/override.coffee',
'atom/renderer/api/lib/ipc.coffee',
'atom/renderer/api/lib/remote.coffee',
'atom/renderer/api/lib/web-view.coffee',
],
'lib_sources': [
'atom/app/atom_main_delegate.cc',
@ -219,6 +220,8 @@
'atom/renderer/api/atom_api_renderer_ipc.cc',
'atom/renderer/api/atom_renderer_bindings.cc',
'atom/renderer/api/atom_renderer_bindings.h',
'atom/renderer/api/atom_api_web_view.cc',
'atom/renderer/api/atom_api_web_view.h',
'atom/renderer/atom_render_view_observer.cc',
'atom/renderer/atom_render_view_observer.h',
'atom/renderer/atom_renderer_client.cc',

View file

@ -22,6 +22,7 @@
#include "base/json/json_writer.h"
#include "base/prefs/pref_service.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
@ -58,6 +59,7 @@ NativeWindow::NativeWindow(content::WebContents* web_contents,
is_closed_(false),
node_integration_("except-iframe"),
has_dialog_attached_(false),
zoom_factor_(1.0),
weak_factory_(this),
inspectable_web_contents_(
brightray::InspectableWebContents::Create(web_contents)) {
@ -82,6 +84,9 @@ NativeWindow::NativeWindow(content::WebContents* web_contents,
if (options->GetDictionary(switches::kWebPreferences, &web_preferences))
web_preferences_.reset(web_preferences->DeepCopy());
// Read the zoom factor before any navigation.
options->GetDouble(switches::kZoomFactor, &zoom_factor_);
web_contents->SetDelegate(this);
inspectable_web_contents()->SetDelegate(this);
@ -358,6 +363,11 @@ void NativeWindow::AppendExtraCommandLineSwitches(CommandLine* command_line,
// Append --node-integration to renderer process.
command_line->AppendSwitchASCII(switches::kNodeIntegration,
node_integration_);
// Append --zoom-factor.
if (zoom_factor_ != 1.0)
command_line->AppendSwitchASCII(switches::kZoomFactor,
base::DoubleToString(zoom_factor_));
}
void NativeWindow::OverrideWebkitPrefs(const GURL& url, WebPreferences* prefs) {

View file

@ -294,9 +294,12 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
// it should be cancelled when we can prove that the window is responsive.
base::CancelableClosure window_unresposive_closure_;
// web preferences.
// Web preferences.
scoped_ptr<base::DictionaryValue> web_preferences_;
// Page's default zoom factor.
double zoom_factor_;
base::WeakPtrFactory<NativeWindow> weak_factory_;
base::WeakPtr<NativeWindow> devtools_window_;

View file

@ -21,6 +21,7 @@ NODE_EXT_LIST_ITEM(atom_browser_window)
// Module names start with `atom_renderer_` can only be used by renderer
// process.
NODE_EXT_LIST_ITEM(atom_renderer_ipc)
NODE_EXT_LIST_ITEM(atom_renderer_web_view)
// Module names start with `atom_common_` can be used by both browser and
// renderer processes.

View file

@ -45,6 +45,9 @@ const char kUseContentSize[] = "use-content-size";
// The WebPreferences.
const char kWebPreferences[] = "web-preferences";
// The factor of which page should be zoomed.
const char kZoomFactor[] = "zoom-factor";
} // namespace switches
} // namespace atom

View file

@ -31,6 +31,7 @@ extern const char kNodeIntegration[];
extern const char kAcceptFirstMouse[];
extern const char kUseContentSize[];
extern const char kWebPreferences[];
extern const char kZoomFactor[];
} // namespace switches

View file

@ -0,0 +1,80 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/renderer/api/atom_api_web_view.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebView.h"
#include "atom/common/node_includes.h"
namespace atom {
namespace api {
namespace {
WebKit::WebView* GetCurrentWebView() {
WebKit::WebFrame* frame = WebKit::WebFrame::frameForCurrentContext();
if (!frame)
return NULL;
return frame->view();
}
} // namespace
WebView::WebView() : web_view_(GetCurrentWebView()) {
}
WebView::~WebView() {
}
double WebView::SetZoomLevel(double level) {
return web_view_->setZoomLevel(level);
}
double WebView::GetZoomLevel() const {
return web_view_->zoomLevel();
}
double WebView::SetZoomFactor(double factor) {
return WebKit::WebView::zoomLevelToZoomFactor(SetZoomLevel(
WebKit::WebView::zoomFactorToZoomLevel(factor)));
}
double WebView::GetZoomFactor() const {
return WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel());
}
mate::ObjectTemplateBuilder WebView::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return mate::ObjectTemplateBuilder(isolate)
.SetMethod("setZoomLevel", &WebView::SetZoomLevel)
.SetMethod("getZoomLevel", &WebView::GetZoomLevel)
.SetMethod("setZoomFactor", &WebView::SetZoomFactor)
.SetMethod("getZoomFactor", &WebView::GetZoomFactor);
}
// static
mate::Handle<WebView> WebView::Create(v8::Isolate* isolate) {
return CreateHandle(isolate, new WebView);
}
} // namespace api
} // namespace atom
namespace {
void Initialize(v8::Handle<v8::Object> exports) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
mate::Dictionary dict(isolate, exports);
dict.Set("webView", atom::api::WebView::Create(isolate));
}
} // namespace
NODE_MODULE(atom_renderer_web_view, Initialize)

View file

@ -0,0 +1,45 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_RENDERER_API_ATOM_API_WEB_VIEW_H_
#define ATOM_RENDERER_API_ATOM_API_WEB_VIEW_H_
#include "native_mate/handle.h"
#include "native_mate/wrappable.h"
namespace WebKit {
class WebView;
}
namespace atom {
namespace api {
class WebView : public mate::Wrappable {
public:
static mate::Handle<WebView> Create(v8::Isolate* isolate);
private:
WebView();
virtual ~WebView();
double SetZoomLevel(double level);
double GetZoomLevel() const;
double SetZoomFactor(double factor);
double GetZoomFactor() const;
// mate::Wrappable:
virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate);
WebKit::WebView* web_view_;
DISALLOW_COPY_AND_ASSIGN(WebView);
};
} // namespace api
} // namespace atom
#endif // ATOM_RENDERER_API_ATOM_API_WEB_VIEW_H_

View file

@ -0,0 +1 @@
module.exports = process.atomBinding('web_view').webView

View file

@ -4,11 +4,15 @@
#include "atom/renderer/atom_render_view_observer.h"
#include <string>
#include <vector>
#include "atom/common/api/api_messages.h"
#include "atom/common/options_switches.h"
#include "atom/renderer/api/atom_renderer_bindings.h"
#include "atom/renderer/atom_renderer_client.h"
#include "base/command_line.h"
#include "base/strings/string_number_conversions.h"
#include "content/public/renderer/render_view.h"
#include "ipc/ipc_message_macros.h"
#include "third_party/WebKit/public/web/WebDraggableRegion.h"
@ -32,6 +36,19 @@ AtomRenderViewObserver::AtomRenderViewObserver(
AtomRenderViewObserver::~AtomRenderViewObserver() {
}
void AtomRenderViewObserver::DidCreateDocumentElement(WebKit::WebFrame* frame) {
// Read --zoom-factor from command line.
std::string zoom_factor_str = CommandLine::ForCurrentProcess()->
GetSwitchValueASCII(switches::kZoomFactor);;
if (zoom_factor_str.empty())
return;
double zoom_factor;
if (!base::StringToDouble(zoom_factor_str, &zoom_factor))
return;
double zoom_level = WebKit::WebView::zoomFactorToZoomLevel(zoom_factor);
frame->view()->setZoomLevel(zoom_level);
}
void AtomRenderViewObserver::DraggableRegionsChanged(WebKit::WebFrame* frame) {
WebKit::WebVector<WebKit::WebDraggableRegion> webregions =
frame->document().draggableRegions();

View file

@ -25,6 +25,7 @@ class AtomRenderViewObserver : public content::RenderViewObserver {
private:
// content::RenderViewObserver implementation.
virtual void DidCreateDocumentElement(WebKit::WebFrame* frame) OVERRIDE;
virtual void DraggableRegionsChanged(WebKit::WebFrame* frame) OVERRIDE;
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;

View file

@ -26,6 +26,7 @@ Modules for web page:
* [ipc (renderer)](api/ipc-renderer.md)
* [remote](api/remote.md)
* [web-view](api/web-view.md)
Modules for both sides:

View file

@ -43,6 +43,8 @@ You can also create a window without chrome by using
other windows
* `fullscreen` Boolean - Whether the window should show in fullscreen
* `skip-taskbar` Boolean - Do not show window in taskbar
* `zoom-factor` Number - The default zoom factor of the page, zoom factor is
zoom percent / 100, so `3.0` represents `300%`
* `kiosk` Boolean - The kiosk mode
* `title` String - Default window title
* `icon` String - The path of icon file

33
docs/api/web-view.md Normal file
View file

@ -0,0 +1,33 @@
# web-view
The `web-view` module can custom the rendering of current web page.
An example of zooming current page to 200%.
```javascript
var webView = require('web-view');
webView.setZoomFactor(2);
```
## webView.setZoomFactor(factor)
* `factor` Number - Zoom factor
Changes the zoom factor to the specified factor, zoom factor is
zoom percent / 100, so 300% = 3.0.
## webView.getZoomFactor()
Returns the current zoom factor.
## webView.setZoomLevel(level)
* `level` Number - Zoom level
Changes the zoom level to the specified level, 0 is "original size", and each
increment above or below represents zooming 20% larger or smaller to default
limits of 300% and 50% of original size, respectively.
## webView.getZoomLevel()
Returns the current zoom level.