Skip to content

Commit

Permalink
Fixed fullscreen title/toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxianqiao committed Jun 29, 2022
1 parent 894342e commit 9ba8450
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
12 changes: 11 additions & 1 deletion packages/tauriapp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extern crate objc;

#[cfg(target_os = "macos")]
use tauri::Menu;
use tauri::{generate_handler, Manager, Window};
use tauri::{generate_handler, Manager, Window, WindowEvent};
#[cfg(target_os = "macos")]
use window_ext::WindowExt;

Expand Down Expand Up @@ -45,6 +45,16 @@ fn main() {
// main_window.open_devtools();
Ok(())
})
.on_window_event(|event| match event.event() {
#[cfg(target_os = "macos")]
WindowEvent::Resized(size) => {
// https://github.com/tauri-apps/tauri/issues/4519
let monitor = event.window().current_monitor().unwrap().unwrap();
let screen = monitor.size();
event.window().set_toolbar_visible(size != screen);
}
_ => {}
})
.on_page_load(|w: Window, _| w.get_window("main").unwrap().show().unwrap())
.plugin(preload::PreloadPlugin::new())
.invoke_handler(generate_handler![play_sound, set_badge])
Expand Down
18 changes: 18 additions & 0 deletions packages/tauriapp/src/window_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use tauri::Window;
pub trait WindowExt {
#[cfg(target_os = "macos")]
fn set_window_style(&self, title_transparent: bool, remove_toolbar: bool);
#[cfg(target_os = "macos")]
fn set_toolbar_visible(&self, visible: bool);
}

impl WindowExt for Window {
Expand Down Expand Up @@ -63,4 +65,20 @@ impl WindowExt for Window {
id.setToolbar_(msg_send![class!(NSToolbar), new]);
}
}

#[cfg(target_os = "macos")]
fn set_toolbar_visible(&self, visible: bool) {
use cocoa::appkit::NSWindow;

unsafe {
let id = self.ns_window().unwrap() as cocoa::base::id;

let v = if visible {
cocoa::base::YES
} else {
cocoa::base::NO
};
let _: cocoa::base::id = msg_send![id.toolbar(), setVisible: v];
}
}
}
14 changes: 10 additions & 4 deletions packages/webapp/src/elements/titlebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const style = createCSSSheet(css`
`);

type State = {
fullscreen: boolean;
maximized: boolean;
blur: boolean;
};
Expand All @@ -79,6 +80,7 @@ export class MTitlebarElement extends GemElement<State> {
@attribute type: 'win' | 'mac';

state: State = {
fullscreen: false,
maximized: false,
blur: false,
};
Expand All @@ -95,8 +97,10 @@ export class MTitlebarElement extends GemElement<State> {
return window.__TAURI__?.window.getCurrent();
}

#isMaximized = () => {
this.#window?.isMaximized().then((maximized) => this.setState({ maximized }));
#onResize = async () => {
const maximized = await this.#window?.isMaximized();
// https://github.com/tauri-apps/tauri/issues/4519
this.setState({ maximized, fullscreen: innerWidth === screen.width && innerHeight === screen.height });
};

#toggleMaximize = () => {
Expand All @@ -115,8 +119,8 @@ export class MTitlebarElement extends GemElement<State> {
setTimeout(() => this.removeEventListener('mousedown', this.#toggleMaximize), 300);
});

this.#isMaximized();
this.#window?.listen('tauri://resize', this.#isMaximized);
this.#onResize();
this.#window?.listen('tauri://resize', this.#onResize);

this.#window?.listen('tauri://blur', () => this.setState({ blur: true }));
this.#window?.listen('tauri://focus', () => this.setState({ blur: false }));
Expand All @@ -132,6 +136,8 @@ export class MTitlebarElement extends GemElement<State> {
}

render = () => {
if (this.state.fullscreen) return html``;

return html`
<dy-reflect>
<style>
Expand Down

0 comments on commit 9ba8450

Please sign in to comment.