refactor: update WebContentsZoomController
(#39428)
refactor: update WebContentsZoomController
This commit is contained in:
parent
5d11c30720
commit
8e3dcc8b17
7 changed files with 305 additions and 121 deletions
|
@ -14,40 +14,49 @@
|
|||
|
||||
namespace electron {
|
||||
|
||||
class WebContentsZoomObserver;
|
||||
|
||||
// Manages the zoom changes of WebContents.
|
||||
class WebContentsZoomController
|
||||
: public content::WebContentsObserver,
|
||||
public content::WebContentsUserData<WebContentsZoomController> {
|
||||
public:
|
||||
class Observer : public base::CheckedObserver {
|
||||
public:
|
||||
virtual void OnZoomLevelChanged(content::WebContents* web_contents,
|
||||
double level,
|
||||
bool is_temporary) {}
|
||||
virtual void OnZoomControllerWebContentsDestroyed() {}
|
||||
|
||||
protected:
|
||||
~Observer() override {}
|
||||
};
|
||||
|
||||
// Defines how zoom changes are handled.
|
||||
enum class ZoomMode {
|
||||
enum ZoomMode {
|
||||
// Results in default zoom behavior, i.e. zoom changes are handled
|
||||
// automatically and on a per-origin basis, meaning that other tabs
|
||||
// navigated to the same origin will also zoom.
|
||||
kDefault,
|
||||
ZOOM_MODE_DEFAULT,
|
||||
// Results in zoom changes being handled automatically, but on a per-tab
|
||||
// basis. Tabs in this zoom mode will not be affected by zoom changes in
|
||||
// other tabs, and vice versa.
|
||||
kIsolated,
|
||||
ZOOM_MODE_ISOLATED,
|
||||
// Overrides the automatic handling of zoom changes. The |onZoomChange|
|
||||
// event will still be dispatched, but the page will not actually be zoomed.
|
||||
// These zoom changes can be handled manually by listening for the
|
||||
// |onZoomChange| event. Zooming in this mode is also on a per-tab basis.
|
||||
kManual,
|
||||
ZOOM_MODE_MANUAL,
|
||||
// Disables all zooming in this tab. The tab will revert to the default
|
||||
// zoom level, and all attempted zoom changes will be ignored.
|
||||
kDisabled,
|
||||
ZOOM_MODE_DISABLED,
|
||||
};
|
||||
|
||||
struct ZoomChangedEventData {
|
||||
ZoomChangedEventData(content::WebContents* web_contents,
|
||||
double old_zoom_level,
|
||||
double new_zoom_level,
|
||||
bool temporary,
|
||||
WebContentsZoomController::ZoomMode zoom_mode)
|
||||
: web_contents(web_contents),
|
||||
old_zoom_level(old_zoom_level),
|
||||
new_zoom_level(new_zoom_level),
|
||||
temporary(temporary),
|
||||
zoom_mode(zoom_mode) {}
|
||||
raw_ptr<content::WebContents> web_contents;
|
||||
double old_zoom_level;
|
||||
double new_zoom_level;
|
||||
bool temporary;
|
||||
WebContentsZoomController::ZoomMode zoom_mode;
|
||||
};
|
||||
|
||||
explicit WebContentsZoomController(content::WebContents* web_contents);
|
||||
|
@ -58,24 +67,29 @@ class WebContentsZoomController
|
|||
WebContentsZoomController& operator=(const WebContentsZoomController&) =
|
||||
delete;
|
||||
|
||||
void AddObserver(Observer* observer);
|
||||
void RemoveObserver(Observer* observer);
|
||||
void AddObserver(WebContentsZoomObserver* observer);
|
||||
void RemoveObserver(WebContentsZoomObserver* observer);
|
||||
|
||||
void SetEmbedderZoomController(WebContentsZoomController* controller);
|
||||
|
||||
// Methods for managing zoom levels.
|
||||
void SetZoomLevel(double level);
|
||||
double GetZoomLevel();
|
||||
// Gets the current zoom level by querying HostZoomMap (if not in manual zoom
|
||||
// mode) or from the ZoomController local value otherwise.
|
||||
double GetZoomLevel() const;
|
||||
|
||||
// Sets the zoom level through HostZoomMap.
|
||||
// Returns true on success.
|
||||
bool SetZoomLevel(double zoom_level);
|
||||
|
||||
void SetDefaultZoomFactor(double factor);
|
||||
double GetDefaultZoomFactor();
|
||||
|
||||
// Sets the temporary zoom level through HostZoomMap.
|
||||
void SetTemporaryZoomLevel(double level);
|
||||
bool UsesTemporaryZoomLevel();
|
||||
|
||||
// Sets the zoom mode, which defines zoom behavior (see enum ZoomMode).
|
||||
void SetZoomMode(ZoomMode zoom_mode);
|
||||
|
||||
void ResetZoomModeOnNavigationIfNeeded(const GURL& url);
|
||||
|
||||
ZoomMode zoom_mode() const { return zoom_mode_; }
|
||||
|
||||
// Convenience method to get default zoom level. Implemented here for
|
||||
|
@ -86,8 +100,9 @@ class WebContentsZoomController
|
|||
}
|
||||
|
||||
protected:
|
||||
// content::WebContentsObserver:
|
||||
void DidFinishNavigation(content::NavigationHandle* handle) override;
|
||||
// content::WebContentsObserver overrides:
|
||||
void DidFinishNavigation(
|
||||
content::NavigationHandle* navigation_handle) override;
|
||||
void WebContentsDestroyed() override;
|
||||
void RenderFrameHostChanged(content::RenderFrameHost* old_host,
|
||||
content::RenderFrameHost* new_host) override;
|
||||
|
@ -95,29 +110,40 @@ class WebContentsZoomController
|
|||
private:
|
||||
friend class content::WebContentsUserData<WebContentsZoomController>;
|
||||
|
||||
// Called after a navigation has committed to set default zoom factor.
|
||||
void ResetZoomModeOnNavigationIfNeeded(const GURL& url);
|
||||
void SetZoomFactorOnNavigationIfNeeded(const GURL& url);
|
||||
void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change);
|
||||
|
||||
// Updates the zoom icon and zoom percentage based on current values and
|
||||
// notifies the observer if changes have occurred. |host| may be empty,
|
||||
// meaning the change should apply to ~all sites. If it is not empty, the
|
||||
// change only affects sites with the given host.
|
||||
void UpdateState(const std::string& host);
|
||||
|
||||
// The current zoom mode.
|
||||
ZoomMode zoom_mode_ = ZoomMode::kDefault;
|
||||
ZoomMode zoom_mode_ = ZOOM_MODE_DEFAULT;
|
||||
|
||||
// Current zoom level.
|
||||
double zoom_level_ = 1.0;
|
||||
// The current zoom level.
|
||||
double zoom_level_;
|
||||
|
||||
// kZoomFactor.
|
||||
double default_zoom_factor_ = 0;
|
||||
|
||||
const double kPageZoomEpsilon = 0.001;
|
||||
// The current default zoom factor.
|
||||
double default_zoom_factor_;
|
||||
|
||||
int old_process_id_ = -1;
|
||||
int old_view_id_ = -1;
|
||||
|
||||
std::unique_ptr<ZoomChangedEventData> event_data_;
|
||||
|
||||
raw_ptr<WebContentsZoomController> embedder_zoom_controller_ = nullptr;
|
||||
|
||||
base::ObserverList<Observer> observers_;
|
||||
// Observer receiving notifications on state changes.
|
||||
base::ObserverList<WebContentsZoomObserver> observers_;
|
||||
|
||||
// Keep track of the HostZoomMap we're currently subscribed to.
|
||||
raw_ptr<content::HostZoomMap> host_zoom_map_;
|
||||
|
||||
base::CallbackListSubscription zoom_subscription_;
|
||||
|
||||
WEB_CONTENTS_USER_DATA_KEY_DECL();
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue