| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/gtk/extensions/shell_window_gtk.h" | 5 #include "chrome/browser/ui/gtk/extensions/shell_window_gtk.h" |
| 6 | 6 |
| 7 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/common/extensions/extension.h" | 8 #include "chrome/common/extensions/extension.h" |
| 9 #include "content/public/browser/render_view_host.h" | 9 #include "content/public/browser/render_view_host.h" |
| 10 #include "content/public/browser/render_widget_host_view.h" | 10 #include "content/public/browser/render_widget_host_view.h" |
| 11 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
| 12 #include "content/public/browser/web_contents_view.h" | 12 #include "content/public/browser/web_contents_view.h" |
| 13 #include "ui/base/x/active_window_watcher_x.h" | 13 #include "ui/base/x/active_window_watcher_x.h" |
| 14 #include "ui/gfx/rect.h" | 14 #include "ui/gfx/rect.h" |
| 15 | 15 |
| 16 namespace { |
| 17 |
| 18 // The timeout in milliseconds before we'll get the true window position with |
| 19 // gtk_window_get_position() after the last GTK configure-event signal. |
| 20 const int kDebounceTimeoutMilliseconds = 100; |
| 21 |
| 22 } // namespace |
| 23 |
| 16 ShellWindowGtk::ShellWindowGtk(ShellWindow* shell_window, | 24 ShellWindowGtk::ShellWindowGtk(ShellWindow* shell_window, |
| 17 const ShellWindow::CreateParams& params) | 25 const ShellWindow::CreateParams& params) |
| 18 : shell_window_(shell_window), | 26 : shell_window_(shell_window), |
| 19 state_(GDK_WINDOW_STATE_WITHDRAWN), | 27 state_(GDK_WINDOW_STATE_WITHDRAWN), |
| 20 is_active_(!ui::ActiveWindowWatcherX::WMSupportsActivation()), | 28 is_active_(!ui::ActiveWindowWatcherX::WMSupportsActivation()), |
| 21 content_thinks_its_fullscreen_(false) { | 29 content_thinks_its_fullscreen_(false) { |
| 22 window_ = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL)); | 30 window_ = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL)); |
| 23 | 31 |
| 24 gfx::NativeView native_view = | 32 gfx::NativeView native_view = |
| 25 web_contents()->GetView()->GetNativeView(); | 33 web_contents()->GetView()->GetNativeView(); |
| 26 gtk_container_add(GTK_CONTAINER(window_), native_view); | 34 gtk_container_add(GTK_CONTAINER(window_), native_view); |
| 27 | 35 |
| 36 if (params.bounds.x() >= 0 && params.bounds.y() >= 0) |
| 37 gtk_window_move(window_, params.bounds.x(), params.bounds.y()); |
| 38 |
| 28 gtk_window_set_default_size( | 39 gtk_window_set_default_size( |
| 29 window_, params.bounds.width(), params.bounds.height()); | 40 window_, params.bounds.width(), params.bounds.height()); |
| 30 | 41 |
| 42 // make sure bounds_ and restored_bounds_ have correct values until we |
| 43 // get our first configure-event |
| 44 bounds_ = restored_bounds_ = params.bounds; |
| 45 gint x, y; |
| 46 gtk_window_get_position(window_, &x, &y); |
| 47 bounds_.set_origin(gfx::Point(x, y)); |
| 48 |
| 31 // Hide titlebar when {frame: 'none'} specified on ShellWindow. | 49 // Hide titlebar when {frame: 'none'} specified on ShellWindow. |
| 32 if (params.frame == ShellWindow::CreateParams::FRAME_NONE) | 50 if (params.frame == ShellWindow::CreateParams::FRAME_NONE) |
| 33 gtk_window_set_decorated(window_, false); | 51 gtk_window_set_decorated(window_, false); |
| 34 | 52 |
| 35 int min_width = params.minimum_size.width(); | 53 int min_width = params.minimum_size.width(); |
| 36 int min_height = params.minimum_size.height(); | 54 int min_height = params.minimum_size.height(); |
| 37 int max_width = params.maximum_size.width(); | 55 int max_width = params.maximum_size.width(); |
| 38 int max_height = params.maximum_size.height(); | 56 int max_height = params.maximum_size.height(); |
| 39 GdkGeometry hints; | 57 GdkGeometry hints; |
| 40 int hints_mask = 0; | 58 int hints_mask = 0; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 void ShellWindowGtk::Show() { | 122 void ShellWindowGtk::Show() { |
| 105 gtk_window_present(window_); | 123 gtk_window_present(window_); |
| 106 } | 124 } |
| 107 | 125 |
| 108 void ShellWindowGtk::ShowInactive() { | 126 void ShellWindowGtk::ShowInactive() { |
| 109 gtk_window_set_focus_on_map(window_, false); | 127 gtk_window_set_focus_on_map(window_, false); |
| 110 gtk_widget_show(GTK_WIDGET(window_)); | 128 gtk_widget_show(GTK_WIDGET(window_)); |
| 111 } | 129 } |
| 112 | 130 |
| 113 void ShellWindowGtk::Close() { | 131 void ShellWindowGtk::Close() { |
| 132 shell_window_->SaveWindowPosition(); |
| 133 |
| 134 // Cancel any pending callback from the window configure debounce timer. |
| 135 window_configure_debounce_timer_.Stop(); |
| 136 |
| 114 GtkWidget* window = GTK_WIDGET(window_); | 137 GtkWidget* window = GTK_WIDGET(window_); |
| 115 // To help catch bugs in any event handlers that might get fired during the | 138 // To help catch bugs in any event handlers that might get fired during the |
| 116 // destruction, set window_ to NULL before any handlers will run. | 139 // destruction, set window_ to NULL before any handlers will run. |
| 117 window_ = NULL; | 140 window_ = NULL; |
| 118 | 141 |
| 119 // OnNativeClose does a delete this so no other members should | 142 // OnNativeClose does a delete this so no other members should |
| 120 // be accessed after. gtk_widget_destroy is safe (and must | 143 // be accessed after. gtk_widget_destroy is safe (and must |
| 121 // be last). | 144 // be last). |
| 122 shell_window_->OnNativeClose(); | 145 shell_window_->OnNativeClose(); |
| 123 gtk_widget_destroy(window); | 146 gtk_widget_destroy(window); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 GdkEvent* event) { | 198 GdkEvent* event) { |
| 176 Close(); | 199 Close(); |
| 177 | 200 |
| 178 // Return true to prevent the GTK window from being destroyed. Close will | 201 // Return true to prevent the GTK window from being destroyed. Close will |
| 179 // destroy it for us. | 202 // destroy it for us. |
| 180 return TRUE; | 203 return TRUE; |
| 181 } | 204 } |
| 182 | 205 |
| 183 gboolean ShellWindowGtk::OnConfigure(GtkWidget* widget, | 206 gboolean ShellWindowGtk::OnConfigure(GtkWidget* widget, |
| 184 GdkEventConfigure* event) { | 207 GdkEventConfigure* event) { |
| 185 // TODO(mihaip): Do we need an explicit gtk_window_get_position call like in | 208 // We update |bounds_| but not |restored_bounds_| here. The latter needs |
| 186 // in BrowserWindowGtk::OnConfigure? | 209 // to be updated conditionally when the window is non-maximized and non- |
| 210 // fullscreen, but whether those state updates have been processed yet is |
| 211 // window-manager specific. We update |restored_bounds_| in the debounced |
| 212 // handler below, after the window state has been updated. |
| 187 bounds_.SetRect(event->x, event->y, event->width, event->height); | 213 bounds_.SetRect(event->x, event->y, event->width, event->height); |
| 188 if (!IsMaximized()) | 214 |
| 215 // The GdkEventConfigure* we get here doesn't have quite the right |
| 216 // coordinates though (they're relative to the drawable window area, rather |
| 217 // than any window manager decorations, if enabled), so we need to call |
| 218 // gtk_window_get_position() to get the right values. (Otherwise session |
| 219 // restore, if enabled, will restore windows to incorrect positions.) That's |
| 220 // a round trip to the X server though, so we set a debounce timer and only |
| 221 // call it (in OnDebouncedBoundsChanged() below) after we haven't seen a |
| 222 // reconfigure event in a short while. |
| 223 // We don't use Reset() because the timer may not yet be running. |
| 224 // (In that case Stop() is a no-op.) |
| 225 window_configure_debounce_timer_.Stop(); |
| 226 window_configure_debounce_timer_.Start(FROM_HERE, |
| 227 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds), this, |
| 228 &ShellWindowGtk::OnDebouncedBoundsChanged); |
| 229 |
| 230 return FALSE; |
| 231 } |
| 232 |
| 233 void ShellWindowGtk::OnDebouncedBoundsChanged() { |
| 234 gint x, y; |
| 235 gtk_window_get_position(window_, &x, &y); |
| 236 bounds_.set_origin(gfx::Point(x, y)); |
| 237 if (!IsFullscreen() && !IsMaximized()) |
| 189 restored_bounds_ = bounds_; | 238 restored_bounds_ = bounds_; |
| 190 | 239 |
| 191 return FALSE; | 240 shell_window_->SaveWindowPosition(); |
| 192 } | 241 } |
| 193 | 242 |
| 194 gboolean ShellWindowGtk::OnWindowState(GtkWidget* sender, | 243 gboolean ShellWindowGtk::OnWindowState(GtkWidget* sender, |
| 195 GdkEventWindowState* event) { | 244 GdkEventWindowState* event) { |
| 196 state_ = event->new_window_state; | 245 state_ = event->new_window_state; |
| 197 | 246 |
| 198 if (content_thinks_its_fullscreen_ && | 247 if (content_thinks_its_fullscreen_ && |
| 199 !(state_ & GDK_WINDOW_STATE_FULLSCREEN)) { | 248 !(state_ & GDK_WINDOW_STATE_FULLSCREEN)) { |
| 200 content_thinks_its_fullscreen_ = false; | 249 content_thinks_its_fullscreen_ = false; |
| 201 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); | 250 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 220 | 269 |
| 221 void ShellWindowGtk::UpdateWindowTitle() { | 270 void ShellWindowGtk::UpdateWindowTitle() { |
| 222 // TODO(jeremya): implement. | 271 // TODO(jeremya): implement. |
| 223 } | 272 } |
| 224 | 273 |
| 225 // static | 274 // static |
| 226 NativeShellWindow* NativeShellWindow::Create( | 275 NativeShellWindow* NativeShellWindow::Create( |
| 227 ShellWindow* shell_window, const ShellWindow::CreateParams& params) { | 276 ShellWindow* shell_window, const ShellWindow::CreateParams& params) { |
| 228 return new ShellWindowGtk(shell_window, params); | 277 return new ShellWindowGtk(shell_window, params); |
| 229 } | 278 } |
| OLD | NEW |