Only define enable/disable API for views

This commit is contained in:
Cheng Zhao 2016-06-20 15:44:50 +09:00
parent e33e4be257
commit 5674e8d114
8 changed files with 64 additions and 98 deletions

View file

@ -326,14 +326,6 @@ bool Window::IsVisible() {
return window_->IsVisible();
}
void Window::Disable() {
window_->Disable();
}
void Window::Enable() {
window_->Enable();
}
bool Window::IsEnabled() {
return window_->IsEnabled();
}
@ -767,8 +759,6 @@ void Window::RemoveFromParentChildWindows() {
return;
parent->child_windows_.Remove(ID());
if (IsModal())
parent->Enable();
}
// static
@ -784,8 +774,6 @@ void Window::BuildPrototype(v8::Isolate* isolate,
.SetMethod("showInactive", &Window::ShowInactive)
.SetMethod("hide", &Window::Hide)
.SetMethod("isVisible", &Window::IsVisible)
.SetMethod("enable", &Window::Enable)
.SetMethod("disable", &Window::Disable)
.SetMethod("isEnabled", &Window::IsEnabled)
.SetMethod("maximize", &Window::Maximize)
.SetMethod("unmaximize", &Window::Unmaximize)
@ -796,7 +784,9 @@ void Window::BuildPrototype(v8::Isolate* isolate,
.SetMethod("setFullScreen", &Window::SetFullScreen)
.SetMethod("isFullScreen", &Window::IsFullscreen)
.SetMethod("setAspectRatio", &Window::SetAspectRatio)
#if !defined(OS_WIN)
.SetMethod("setParentWindow", &Window::SetParentWindow)
#endif
.SetMethod("getParentWindow", &Window::GetParentWindow)
.SetMethod("getChildWindows", &Window::GetChildWindows)
.SetMethod("isModal", &Window::IsModal)

View file

@ -99,8 +99,6 @@ class Window : public mate::TrackableObject<Window>,
void ShowInactive();
void Hide();
bool IsVisible();
void Disable();
void Enable();
bool IsEnabled();
void Maximize();
void Unmaximize();

View file

@ -57,7 +57,6 @@ NativeWindow::NativeWindow(
sheet_offset_x_(0.0),
sheet_offset_y_(0.0),
aspect_ratio_(0.0),
disable_count_(0),
parent_(parent),
is_modal_(false),
inspectable_web_contents_(inspectable_web_contents),
@ -185,18 +184,6 @@ void NativeWindow::InitFromOptions(const mate::Dictionary& options) {
Show();
}
void NativeWindow::Disable() {
++disable_count_;
if (disable_count_ == 1)
SetEnabled(false);
}
void NativeWindow::Enable() {
--disable_count_;
if (disable_count_ == 0)
SetEnabled(true);
}
void NativeWindow::SetSize(const gfx::Size& size, bool animate) {
SetBounds(gfx::Rect(GetPosition(), size), animate);
}

View file

@ -98,9 +98,6 @@ class NativeWindow : public base::SupportsUserData,
virtual void ShowInactive() = 0;
virtual void Hide() = 0;
virtual bool IsVisible() = 0;
virtual void Disable();
virtual void Enable();
virtual void SetEnabled(bool enable) = 0; // internal API, should not be used
virtual bool IsEnabled() = 0;
virtual void Maximize() = 0;
virtual void Unmaximize() = 0;
@ -339,9 +336,6 @@ class NativeWindow : public base::SupportsUserData,
double aspect_ratio_;
gfx::Size aspect_ratio_extraSize_;
// How many times the Disable has been called.
int disable_count_;
// The parent window, it is guaranteed to be valid during this window's life.
NativeWindow* parent_;

View file

@ -35,7 +35,6 @@ class NativeWindowMac : public NativeWindow {
void ShowInactive() override;
void Hide() override;
bool IsVisible() override;
void SetEnabled(bool enable) override;
bool IsEnabled() override;
void Maximize() override;
void Unmaximize() override;

View file

@ -279,7 +279,6 @@ bool ScopedDisableResize::disable_resize_ = false;
@property BOOL acceptsFirstMouse;
@property BOOL disableAutoHideCursor;
@property BOOL disableKeyOrMainWindow;
@property BOOL disableMouseEvents;
- (void)setShell:(atom::NativeWindowMac*)shell;
- (void)setEnableLargerThanScreen:(bool)enable;
@ -349,29 +348,6 @@ bool ScopedDisableResize::disable_resize_ = false;
return !self.disableKeyOrMainWindow;
}
- (void)sendEvent:(NSEvent*)event {
// Drop all mouse events.
if (self.disableMouseEvents) {
switch([event type]) {
case NSLeftMouseUp:
case NSLeftMouseDown:
case NSRightMouseDown:
case NSRightMouseUp:
case NSOtherMouseUp:
case NSLeftMouseDragged:
case NSRightMouseDragged:
case NSOtherMouseDragged:
case NSMouseMoved:
case NSScrollWheel:
return;
default:
break;
}
}
[super sendEvent:event];
}
@end
@interface ControlRegionView : NSView
@ -521,7 +497,6 @@ NativeWindowMac::NativeWindowMac(
backing:NSBackingStoreBuffered
defer:YES]);
[window_ setShell:this];
[window_ setDisableMouseEvents:NO];
[window_ setEnableLargerThanScreen:enable_larger_than_screen()];
window_delegate_.reset([[AtomNSWindowDelegate alloc] initWithShell:this]);
@ -692,11 +667,6 @@ bool NativeWindowMac::IsVisible() {
return [window_ isVisible];
}
void NativeWindowMac::SetEnabled(bool enable) {
[window_ setDisableKeyOrMainWindow:!enable];
[window_ setDisableMouseEvents:!enable];
}
bool NativeWindowMac::IsEnabled() {
return [window_ attachedSheet] == nil;
}

View file

@ -129,13 +129,14 @@ NativeWindowViews::NativeWindowViews(
brightray::InspectableWebContents* web_contents,
const mate::Dictionary& options,
NativeWindow* parent)
: NativeWindow(web_contents, options),
: NativeWindow(web_contents, options, parent),
window_(new views::Widget),
web_view_(inspectable_web_contents()->GetView()->GetView()),
menu_bar_autohide_(false),
menu_bar_visible_(false),
menu_bar_alt_pressed_(false),
keyboard_event_handler_(new views::UnhandledKeyboardEventHandler),
disable_count_(0),
use_content_size_(false),
movable_(true),
resizable_(true),
@ -242,16 +243,24 @@ NativeWindowViews::NativeWindowViews(
state_atom_list.push_back(GetAtom("_NET_WM_STATE_FULLSCREEN"));
}
std::string window_type;
options.Get(options::kType, &window_type);
if (parent) {
SetParentWindow(parent);
// Force using dialog type for child window.
window_type = "dialog";
// Modal window needs the _NET_WM_STATE_MODAL hint.
if (is_modal())
state_atom_list.push_back(GetAtom("_NET_WM_STATE_MODAL"));
}
ui::SetAtomArrayProperty(GetAcceleratedWidget(), "_NET_WM_STATE", "ATOM",
state_atom_list);
// Set the _NET_WM_WINDOW_TYPE.
std::string window_type;
if (options.Get(options::kType, &window_type))
if (!window_type.empty())
SetWindowType(GetAcceleratedWidget(), window_type);
if (parent)
SetParentWindow(parent);
#endif
// Add web view.
@ -346,6 +355,9 @@ bool NativeWindowViews::IsFocused() {
}
void NativeWindowViews::Show() {
if (is_modal() && NativeWindow::parent())
static_cast<NativeWindowViews*>(NativeWindow::parent())->SetEnabled(false);
window_->native_widget_private()->ShowWithWindowState(GetRestoredState());
NotifyWindowShow();
@ -368,6 +380,9 @@ void NativeWindowViews::ShowInactive() {
}
void NativeWindowViews::Hide() {
if (is_modal() && NativeWindow::parent())
static_cast<NativeWindowViews*>(NativeWindow::parent())->SetEnabled(true);
window_->Hide();
NotifyWindowHide();
@ -382,22 +397,6 @@ bool NativeWindowViews::IsVisible() {
return window_->IsVisible();
}
void NativeWindowViews::SetEnabled(bool enable) {
#if defined(OS_WIN)
::EnableWindow(GetAcceleratedWidget(), enable);
#elif defined(USE_X11)
views::DesktopWindowTreeHostX11* tree_host =
views::DesktopWindowTreeHostX11::GetHostForXID(GetAcceleratedWidget());
if (enable) {
tree_host->RemoveEventRewriter(event_disabler_.get());
event_disabler_.reset();
} else {
event_disabler_.reset(new EventDisabler);
tree_host->AddEventRewriter(event_disabler_.get());
}
#endif
}
bool NativeWindowViews::IsEnabled() {
#if defined(OS_WIN)
return ::IsWindowEnabled(GetAcceleratedWidget());
@ -831,16 +830,6 @@ void NativeWindowViews::SetParentWindow(NativeWindow* parent) {
#endif
}
void NativeWindowViews::SetModal(bool modal) {
NativeWindow::SetModal(modal);
#if defined(USE_X11)
SetWindowType(GetAcceleratedWidget(), modal ? "dialog" : "normal");
Show();
SetWMSpecState(GetAcceleratedWidget(), modal,
GetAtom("_NET_WM_STATE_MODAL"));
#endif
}
gfx::NativeWindow NativeWindowViews::GetNativeWindow() {
return window_->GetNativeWindow();
}
@ -936,6 +925,33 @@ void NativeWindowViews::SetIcon(const gfx::ImageSkia& icon) {
}
#endif
void NativeWindowViews::SetEnabled(bool enable) {
// Handle multiple calls of SetEnabled correctly.
if (enable) {
--disable_count_;
if (disable_count_ != 0)
return;
} else {
++disable_count_;
if (disable_count_ != 1)
return;
}
#if defined(OS_WIN)
::EnableWindow(GetAcceleratedWidget(), enable);
#elif defined(USE_X11)
views::DesktopWindowTreeHostX11* tree_host =
views::DesktopWindowTreeHostX11::GetHostForXID(GetAcceleratedWidget());
if (enable) {
tree_host->RemoveEventRewriter(event_disabler_.get());
event_disabler_.reset();
} else {
event_disabler_.reset(new EventDisabler);
tree_host->AddEventRewriter(event_disabler_.get());
}
#endif
}
void NativeWindowViews::OnWidgetActivationChanged(
views::Widget* widget, bool active) {
if (widget != window_.get())
@ -969,6 +985,15 @@ void NativeWindowViews::OnWidgetBoundsChanged(
}
void NativeWindowViews::DeleteDelegate() {
if (is_modal() && NativeWindow::parent()) {
NativeWindowViews* parent =
static_cast<NativeWindowViews*>(NativeWindow::parent());
// Enable parent window after current window gets closed.
parent->SetEnabled(true);
// Focus on parent window.
parent->Focus(true);
}
NotifyWindowClosed();
}

View file

@ -57,7 +57,6 @@ class NativeWindowViews : public NativeWindow,
void ShowInactive() override;
void Hide() override;
bool IsVisible() override;
void SetEnabled(bool enable) override;
bool IsEnabled() override;
void Maximize() override;
void Unmaximize() override;
@ -100,7 +99,6 @@ class NativeWindowViews : public NativeWindow,
void SetFocusable(bool focusable) override;
void SetMenu(ui::MenuModel* menu_model) override;
void SetParentWindow(NativeWindow* parent) override;
void SetModal(bool modal) override;
gfx::NativeWindow GetNativeWindow() override;
void SetOverlayIcon(const gfx::Image& overlay,
const std::string& description) override;
@ -120,6 +118,8 @@ class NativeWindowViews : public NativeWindow,
void SetIcon(const gfx::ImageSkia& icon);
#endif
void SetEnabled(bool enable);
views::Widget* widget() const { return window_.get(); }
#if defined(OS_WIN)
@ -230,6 +230,9 @@ class NativeWindowViews : public NativeWindow,
// Map from accelerator to menu item's command id.
accelerator_util::AcceleratorTable accelerator_table_;
// How many times the Disable has been called.
int disable_count_;
bool use_content_size_;
bool movable_;
bool resizable_;