window_state_watcher.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/ui/x/window_state_watcher.h"
  5. #include <X11/Xlib.h>
  6. #include "ui/events/platform/platform_event_source.h"
  7. #include "ui/gfx/x/x11_atom_cache.h"
  8. namespace atom {
  9. WindowStateWatcher::WindowStateWatcher(NativeWindowViews* window)
  10. : window_(window), widget_(window->GetAcceleratedWidget()) {
  11. ui::PlatformEventSource::GetInstance()->AddPlatformEventObserver(this);
  12. }
  13. WindowStateWatcher::~WindowStateWatcher() {
  14. ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this);
  15. }
  16. void WindowStateWatcher::WillProcessEvent(const ui::PlatformEvent& event) {
  17. if (IsWindowStateEvent(event)) {
  18. was_minimized_ = window_->IsMinimized();
  19. was_maximized_ = window_->IsMaximized();
  20. }
  21. }
  22. void WindowStateWatcher::DidProcessEvent(const ui::PlatformEvent& event) {
  23. if (IsWindowStateEvent(event)) {
  24. bool is_minimized = window_->IsMinimized();
  25. bool is_maximized = window_->IsMaximized();
  26. bool is_fullscreen = window_->IsFullscreen();
  27. if (is_minimized != was_minimized_) {
  28. if (is_minimized)
  29. window_->NotifyWindowMinimize();
  30. else
  31. window_->NotifyWindowRestore();
  32. } else if (is_maximized != was_maximized_) {
  33. if (is_maximized)
  34. window_->NotifyWindowMaximize();
  35. else
  36. window_->NotifyWindowUnmaximize();
  37. } else {
  38. // If this is neither a "maximize" or "minimize" event, then we think it
  39. // is a "fullscreen" event.
  40. // The "IsFullscreen()" becomes true immediately before "WillProcessEvent"
  41. // is called, so we can not handle this like "maximize" and "minimize" by
  42. // watching whether they have changed.
  43. if (is_fullscreen)
  44. window_->NotifyWindowEnterFullScreen();
  45. else
  46. window_->NotifyWindowLeaveFullScreen();
  47. }
  48. }
  49. }
  50. bool WindowStateWatcher::IsWindowStateEvent(const ui::PlatformEvent& event) {
  51. ::Atom changed_atom = event->xproperty.atom;
  52. return (changed_atom == gfx::GetAtom("_NET_WM_STATE") &&
  53. event->type == PropertyNotify && event->xproperty.window == widget_);
  54. }
  55. } // namespace atom