Skip to content

Commit

Permalink
Merge pull request nwjs#1076 from richardcypher/skiptb
Browse files Browse the repository at this point in the history
[MAC]implement on mac: hide the window from task bar while keeping it shown
  • Loading branch information
rogerwang committed Feb 19, 2014
2 parents 206830e + f14374b commit d8e1d48
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/api/window/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ void Window::Call(const std::string& method,
bool top;
if (arguments.GetBoolean(0, &top))
shell_->window()->SetAlwaysOnTop(top);
} else if (method == "SetShowInTaskbar" ) {
bool show;
if (arguments.GetBoolean(0, &show))
shell_->window()->SetShowInTaskbar(show);
} else if (method == "MoveTo") {
int x, y;
if (arguments.GetInteger(0, &x) &&
Expand Down
5 changes: 5 additions & 0 deletions src/api/window_bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@ Window.prototype.setAlwaysOnTop = function(flag) {
CallObjectMethod(this, 'SetAlwaysOnTop', [ Boolean(flag) ]);
}

Window.prototype.setShowInTaskbar = function(flag) {
flag = Boolean(flag);
CallObjectMethod(this, 'SetShowInTaskbar', [ flag ]);
}

Window.prototype.requestAttention = function(flash) {
flash = Boolean(flash);
CallObjectMethod(this, 'RequestAttention', [ flash ]);
Expand Down
5 changes: 5 additions & 0 deletions src/browser/native_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ void NativeWindow::InitFromManifest(base::DictionaryValue* manifest) {
if (manifest->GetBoolean(switches::kmAlwaysOnTop, &top) && top) {
SetAlwaysOnTop(true);
}
bool showInTaskbar;
if (manifest->GetBoolean(switches::kmShowInTaskbar, &showInTaskbar) &&
!showInTaskbar) {
SetShowInTaskbar(false);
}
bool fullscreen;
if (manifest->GetBoolean(switches::kmFullscreen, &fullscreen) && fullscreen) {
SetFullscreen(true);
Expand Down
1 change: 1 addition & 0 deletions src/browser/native_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class NativeWindow {
virtual void SetMaximumSize(int width, int height) = 0;
virtual void SetResizable(bool resizable) = 0;
virtual void SetAlwaysOnTop(bool top) = 0;
virtual void SetShowInTaskbar(bool show = true) = 0;
virtual void SetPosition(const std::string& position) = 0;
virtual void SetPosition(const gfx::Point& position) = 0;
virtual gfx::Point GetPosition() = 0;
Expand Down
4 changes: 4 additions & 0 deletions src/browser/native_window_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ void NativeWindowGtk::SetAlwaysOnTop(bool top) {
gtk_window_set_keep_above(window_, top ? TRUE : FALSE);
}

void NativeWindowGtk::SetShowInTaskbar(bool show) {
gtk_window_set_skip_taskbar_hint(window_, show ? FALSE : TRUE);
}

void NativeWindowGtk::SetPosition(const std::string& position) {
if (position == "center")
gtk_window_set_position(window_, GTK_WIN_POS_CENTER);
Expand Down
1 change: 1 addition & 0 deletions src/browser/native_window_gtk.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class NativeWindowGtk : public NativeWindow {
virtual void SetMaximumSize(int width, int height) OVERRIDE;
virtual void SetResizable(bool resizable) OVERRIDE;
virtual void SetAlwaysOnTop(bool top) OVERRIDE;
virtual void SetShowInTaskbar(bool show = true) OVERRIDE;
virtual void SetPosition(const std::string& position) OVERRIDE;
virtual void SetPosition(const gfx::Point& position) OVERRIDE;
virtual gfx::Point GetPosition() OVERRIDE;
Expand Down
1 change: 1 addition & 0 deletions src/browser/native_window_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class NativeWindowCocoa : public NativeWindow {
virtual void SetMaximumSize(int width, int height) OVERRIDE;
virtual void SetResizable(bool resizable) OVERRIDE;
virtual void SetAlwaysOnTop(bool top) OVERRIDE;
virtual void SetShowInTaskbar(bool show = true) OVERRIDE;
virtual void SetPosition(const std::string& position) OVERRIDE;
virtual void SetPosition(const gfx::Point& position) OVERRIDE;
virtual gfx::Point GetPosition() OVERRIDE;
Expand Down
16 changes: 16 additions & 0 deletions src/browser/native_window_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,22 @@ - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
[window() setLevel:(top ? NSFloatingWindowLevel : NSNormalWindowLevel)];
}

void NativeWindowCocoa::SetShowInTaskbar(bool show) {
ProcessSerialNumber psn = { 0, kCurrentProcess };
if (!show) {
NSArray* windowList = [[NSArray alloc] init];
windowList = [NSWindow windowNumbersWithOptions:NSWindowNumberListAllSpaces];
for (unsigned int i = 0; i < [windowList count]; ++i) {
NSWindow *window = [NSApp windowWithWindowNumber:[[windowList objectAtIndex:i] integerValue]];
[window setCanHide:NO];
}
TransformProcessType(&psn, kProcessTransformToUIElementApplication);
}
else {
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
}
}

void NativeWindowCocoa::SetPosition(const std::string& position) {
if (position == "center")
[window() center];
Expand Down
43 changes: 42 additions & 1 deletion src/browser/native_window_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@

#include "content/nw/src/browser/native_window_win.h"

#include <shobjidl.h>

#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "base/win/scoped_comptr.h"
#include "base/win/windows_version.h"
#include "base/win/wrapped_window_proc.h"
#include "chrome/browser/platform_util.h"
#include "content/nw/src/api/menu/menu.h"
Expand Down Expand Up @@ -370,9 +374,46 @@ void NativeWindowWin::SetResizable(bool resizable) {
::SetWindowLong((HWND)window_->GetNativeView(), GWL_STYLE, style);
}

void NativeWindowWin::SetShowInTaskbar(bool show) {
if (show == false && base::win::GetVersion() < base::win::VERSION_VISTA) {
if (hidden_owner_window_.get() == NULL) {
hidden_owner_window_.reset(new HiddenOwnerWindow());
}

// Change the owner of native window. Only needed on Windows XP.
::SetWindowLong(window_->GetNativeView(),
GWL_HWNDPARENT,
(LONG)hidden_owner_window_->hwnd());
}

base::win::ScopedComPtr<ITaskbarList> taskbar;
HRESULT result = taskbar.CreateInstance(CLSID_TaskbarList, NULL,
CLSCTX_INPROC_SERVER);
if (FAILED(result)) {
VLOG(1) << "Failed creating a TaskbarList object: " << result;
return;
}

result = taskbar->HrInit();
if (FAILED(result)) {
LOG(ERROR) << "Failed initializing an ITaskbarList interface.";
return;
}

if (show)
result = taskbar->AddTab(window_->GetNativeWindow());
else
result = taskbar->DeleteTab(window_->GetNativeWindow());

if (FAILED(result)) {
LOG(ERROR) << "Failed to change the show in taskbar attribute";
return;
}
}

void NativeWindowWin::SetAlwaysOnTop(bool top) {
window_->StackAtTop();
// SetAlwaysOnTop should be called after StackAtTop because otherwise
// SetAlwaysOnTop should be called after StackAtTop because otherwise
// the top-most flag will be removed.
window_->SetAlwaysOnTop(top);
}
Expand Down
31 changes: 31 additions & 0 deletions src/browser/native_window_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "content/nw/src/browser/native_window.h"

#include "third_party/skia/include/core/SkRegion.h"
#include "ui/base/win/window_impl.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/rect.h"
#include "ui/views/focus/widget_focus_manager.h"
Expand All @@ -38,6 +39,31 @@ namespace nw {

class NativeWindowToolbarWin;

///////////////////////////////////////////////////////////////////////////////
//
// HiddenOwnerWindow
// This class is used as a hidden owner window for NativeWindowWin.
// Note: The reason for using it is on Windows XP, while using the
// ITaskbarList::DeleteTab to remove the icon from the taskbar, the icon will
// appear in taskbar again when it blur and being focused again. This class
// object will only exist on Windows XP. See the implementation about
// |NativeWindowWin::SetShowInTaskbar|.
//
///////////////////////////////////////////////////////////////////////////////
class HiddenOwnerWindow : public ui::WindowImpl {
public:
HiddenOwnerWindow() {
Init(NULL, gfx::Rect());
}

~HiddenOwnerWindow() {
DestroyWindow(hwnd());
}

BEGIN_MSG_MAP_EX(HiddenOwnerWindow)
END_MSG_MAP()
};

class NativeWindowWin : public NativeWindow,
public views::WidgetFocusChangeListener,
public views::WidgetDelegateView ,
Expand Down Expand Up @@ -69,6 +95,7 @@ class NativeWindowWin : public NativeWindow,
virtual void SetMaximumSize(int width, int height) OVERRIDE;
virtual void SetResizable(bool resizable) OVERRIDE;
virtual void SetAlwaysOnTop(bool top) OVERRIDE;
virtual void SetShowInTaskbar(bool show = true) OVERRIDE;
virtual void SetPosition(const std::string& position) OVERRIDE;
virtual void SetPosition(const gfx::Point& position) OVERRIDE;
virtual gfx::Point GetPosition() OVERRIDE;
Expand Down Expand Up @@ -149,6 +176,10 @@ class NativeWindowWin : public NativeWindow,

scoped_ptr<SkRegion> draggable_region_;

// This is only useful on Windows XP. Hidden owner window for Windows XP to
// let SetShowInTaskbar work like on Windows 7.
scoped_ptr<HiddenOwnerWindow> hidden_owner_window_;

// The window's menubar.
nwapi::Menu* menu_;

Expand Down
3 changes: 3 additions & 0 deletions src/common/shell_switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ const char kmAsDesktop[] = "as_desktop";
const char kmFullscreen[] = "fullscreen";
const char kmInitialFocus[] = "focus";

// Make windows icon hide show or hide in taskbar.
const char kmShowInTaskbar[] = "show_in_taskbar";

// Start with the kiosk mode, see Opera's page for description:
// http://www.opera.com/support/mastering/kiosk/
const char kmKiosk[] = "kiosk";
Expand Down
1 change: 1 addition & 0 deletions src/common/shell_switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ extern const char kmMaxHeight[];
extern const char kmResizable[];
extern const char kmAsDesktop[];
extern const char kmFullscreen[];
extern const char kmShowInTaskbar[];
extern const char kmKiosk[];
extern const char kmAlwaysOnTop[];
extern const char kmInitialFocus[];
Expand Down

0 comments on commit d8e1d48

Please sign in to comment.