Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc tauri fixes #503

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 63 additions & 27 deletions app/tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::sync::Mutex;
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
use tauri::{Builder, PhysicalSize, Runtime, Wry};
use tauri::{Builder, LogicalSize, Runtime, Wry};

#[non_exhaustive]
struct WindowSize;
Expand All @@ -12,13 +12,18 @@ use crate::system_tray;
use crate::updater;

impl WindowSize {

// Not sure why, if its due to UI scaling or what though these values seem to size smaller than the same values on electron
pub const MIN: PhysicalSize<u32> = PhysicalSize { width : 420, height: 602};
pub const COMPACT: PhysicalSize<u32> = PhysicalSize { width : 420, height: 80};
pub const MIN: LogicalSize<u32> = LogicalSize {
width: 420,
height: 602,
};
pub const COMPACT: LogicalSize<u32> = LogicalSize {
width: 420,
height: 80,
};
}

static ORIGINAL_SIZE: Mutex<PhysicalSize<u32>> = Mutex::new(WindowSize::MIN);
static ORIGINAL_SIZE: Mutex<LogicalSize<u32>> = Mutex::new(WindowSize::MIN);

static HAS_DECORATIONS: Mutex<bool> = Mutex::new(true);
static IS_COMPACT: Mutex<bool> = Mutex::new(false);
Expand Down Expand Up @@ -55,8 +60,15 @@ fn set_always_on_top<R: Runtime>(always_on_top: bool, window: tauri::Window<R>)
}

#[tauri::command]
fn set_fullscreen_break<R: Runtime>(should_fullscreen: bool, always_on_top: bool, window: tauri::Window<R>) {
println!("set_fullscreen_break! {} {}", should_fullscreen, always_on_top);
fn set_fullscreen_break<R: Runtime>(
should_fullscreen: bool,
always_on_top: bool,
window: tauri::Window<R>,
) {
println!(
"set_fullscreen_break! {} {}",
should_fullscreen, always_on_top
);
try_set_always_on_top(always_on_top, &window);
if should_fullscreen {
try_set_native_titlebar(true, &window);
Expand Down Expand Up @@ -84,14 +96,15 @@ fn try_set_always_on_top<R: Runtime>(always_on_top: bool, window: &tauri::Window
}
}

fn try_set_min_size<R: Runtime>(size: Option<PhysicalSize<u32>>, window: &tauri::Window<R>) {
match window.set_min_size(size) {//Some(size)) {
fn try_set_min_size<R: Runtime>(size: Option<LogicalSize<u32>>, window: &tauri::Window<R>) {
match window.set_min_size(size) {
//Some(size)) {
Ok(_) => (),
Err(e) => println!("There was a problem setting min size! {:?}", e),
}
}

fn try_set_size<R: Runtime>(size: PhysicalSize<u32>, window: &tauri::Window<R>) {
fn try_set_size<R: Runtime>(size: LogicalSize<u32>, window: &tauri::Window<R>) {
match window.set_size(size) {
Ok(_) => (),
Err(e) => println!("There was a problem setting the window size! {:?}", e),
Expand All @@ -103,27 +116,33 @@ fn try_set_resizeable<R: Runtime>(resizeable: bool, window: &tauri::Window<R>) {
println!("Window resizeable {:?}", resizeable);
match window.set_resizable(resizeable) {
Ok(_) => (),
Err(e) => println!("There was a problem making the window resizeable {:?}", e)
Err(e) => println!("There was a problem making the window resizeable {:?}", e),
}
}

// Atm resizeable seems to cause a problem where the size cannot be set smaller than a certain amount
// https://github.com/tauri-apps/tao/issues/561, just use resizeable until this is fixed.
fn set_window_fixed_size<R: Runtime>(size: PhysicalSize<u32>, window: &tauri::Window<R>) {
fn set_window_fixed_size<R: Runtime>(size: LogicalSize<u32>, window: &tauri::Window<R>) {
let decorations = HAS_DECORATIONS.lock().unwrap();
let new_height = size.height + (if *decorations { 0 } else { 34 });
let new_size = PhysicalSize { width : size.width, height: new_height};
let new_size = LogicalSize {
width: size.width,
height: new_height,
};
try_set_size(new_size, window);
try_set_resizeable(false, window);
}

fn set_window_resizeable<R: Runtime>(min_size: PhysicalSize<u32>, size: PhysicalSize<u32>, window: &tauri::Window<R>) {
fn set_window_resizeable<R: Runtime>(
min_size: LogicalSize<u32>,
size: LogicalSize<u32>,
window: &tauri::Window<R>,
) {
try_set_resizeable(true, window);
try_set_size(size, window);
try_set_min_size(Some(min_size), window);
}


#[tauri::command]
fn set_compact_mode<R: Runtime>(compact_mode: bool, window: tauri::Window<R>) {
{
Expand All @@ -134,11 +153,18 @@ fn set_compact_mode<R: Runtime>(compact_mode: bool, window: tauri::Window<R>) {
if compact_mode {
{
let mut size = ORIGINAL_SIZE.lock().unwrap();
*size = window.outer_size().unwrap().clone();
*size = window
.outer_size()
.unwrap()
.to_logical(window.scale_factor().unwrap());
}
set_window_fixed_size(WindowSize::COMPACT, &window);
} else {
set_window_resizeable(WindowSize::MIN, ORIGINAL_SIZE.lock().unwrap().clone(), &window);
set_window_resizeable(
WindowSize::MIN,
ORIGINAL_SIZE.lock().unwrap().clone(),
&window,
);
}
}

Expand All @@ -164,28 +190,38 @@ fn set_native_titlebar<R: Runtime>(use_native_titlebar: bool, window: tauri::Win
fn try_set_native_titlebar<R: Runtime>(use_native_titlebar: bool, window: &tauri::Window<R>) {
match window.set_decorations(use_native_titlebar) {
Ok(_) => (),
Err(e) => println!("There was a problem setting the window decorations! {:?}", e),
Err(e) => println!(
"There was a problem setting the window decorations! {:?}",
e
),
}
println!("set_native_titlebar! {}", use_native_titlebar);
}


/**
* We could do this by passing the object into a custom function that adds the commands but I wanted
* to practice more with rust. Plus it makes the setup cleaner.
* We could do this by passing the object into a custom function that adds the commands but I wanted
* to practice more with rust. Plus it makes the setup cleaner.

* Switch to a function that takes and returns tauri::Builder<Wry> or uses a reference if we need to
* switch it.
*/
* Switch to a function that takes and returns tauri::Builder<Wry> or uses a reference if we need to
* switch it.
*/
pub trait PomatezCommands {
fn register_pomatez_commands(self) -> tauri::Builder<Wry>;
}

impl PomatezCommands for Builder<Wry> {
fn register_pomatez_commands(self) -> tauri::Builder<Wry> {
self.invoke_handler(tauri::generate_handler![set_show, set_always_on_top,
set_fullscreen_break, set_compact_mode, set_ui_theme, set_native_titlebar,
system_tray::tray_icon_update, set_close, set_minimize, updater::check_for_updates,
self.invoke_handler(tauri::generate_handler![
set_show,
set_always_on_top,
set_fullscreen_break,
set_compact_mode,
set_ui_theme,
set_native_titlebar,
system_tray::tray_icon_update,
set_close,
set_minimize,
updater::check_for_updates,
updater::install_update
])
}
Expand Down
22 changes: 15 additions & 7 deletions app/tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]

#[cfg(debug_assertions)]
use tauri::{Manager};
use tauri::{RunEvent};
use tauri::Manager;
use tauri::RunEvent;
use tauri_plugin_autostart::MacosLauncher;
use tauri_plugin_window;

#[macro_use]
mod commands;
mod system_tray;
mod global_shortcuts;
mod main_window;
mod system_tray;
mod updater;

use commands::PomatezCommands;
use global_shortcuts::{PomatezGlobalShortcutsRegister, PomatezGlobalShortcutsSetup};
use system_tray::PomatezTray;
use global_shortcuts::{PomatezGlobalShortcutsSetup, PomatezGlobalShortcutsRegister};

fn main() {
let app = tauri::Builder::default()
.plugin(tauri_plugin_autostart::init(MacosLauncher::LaunchAgent, None))
.plugin(tauri_plugin_autostart::init(
MacosLauncher::LaunchAgent,
None,
))
.plugin(tauri_plugin_window::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_notification::init())
Expand All @@ -30,6 +34,7 @@ fn main() {
.setup(|app| {
#[cfg(desktop)]
{
main_window::create_main_window(&app.handle());
app.setup_global_shortcuts();
app.set_pomatez_system_tray();
}
Expand All @@ -49,6 +54,9 @@ fn main() {

println!("Pomatez is ready");
}
RunEvent::ExitRequested { api, .. } => {
api.prevent_exit();
Copy link
Member

@sekwah41 sekwah41 Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I'd already implemented the closing to tray in the settings?

Or are you talking about the Mac's close to tray? If so, you'll need to add a check or a compiler flag to make sure it doesn't do this on Windows as that is not the expected behavior.

Ill be honest I've only lightly tested it on a very old mac so id just like to outline the current behavior vs expected.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this implemented for these other platforms on tauri? I cannot find any code that handles this for any platform

Copy link
Member

@sekwah41 sekwah41 Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe they expect apps to handle it themselves, I tauri just provides a platform to make apps. The same as electron doesn't implement this behavior either.

}
_ => {}
});
}
24 changes: 24 additions & 0 deletions app/tauri/src/main_window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use tauri::{AppHandle, Manager, WindowBuilder};

pub static MAIN_WINDOW_ID: &str = "main";

pub fn create_main_window(handle: &AppHandle) {
if handle.get_window(MAIN_WINDOW_ID).is_some() {
return;
}
let window = WindowBuilder::new(handle, MAIN_WINDOW_ID, Default::default())
.inner_size(340f64, 502f64)
.resizable(true)
.title("pomatez")
.build()
.unwrap();

let window_clone = window.clone();
window.on_window_event(move |e| match e {
tauri::WindowEvent::CloseRequested { api, .. } => {
api.prevent_close();
window_clone.hide().unwrap();
}
_ => {}
})
}
29 changes: 19 additions & 10 deletions app/tauri/src/system_tray.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::path::PathBuf;
use tauri::{App, Icon, Manager, Runtime};
use tauri::path::BaseDirectory;
use tauri::{
menu::{MenuBuilder, MenuItemBuilder},
tray::{ClickType, TrayIconBuilder},
};
use tauri::path::BaseDirectory;
use tauri::{App, Icon, Manager, Runtime};

use base64;
use base64::Engine;
use base64::engine::general_purpose;
use base64::Engine;

use crate::main_window;

#[tauri::command]
pub fn tray_icon_update<R: Runtime>(data_url: String, window: tauri::Window<R>) {
Expand All @@ -33,7 +35,6 @@ pub fn tray_icon_update<R: Runtime>(data_url: String, window: tauri::Window<R>)
}
}


/**
* We could do this by passing the object into a custom function that adds the commands but I wanted
* to practice more with rust. Plus it makes the setup cleaner.
Expand All @@ -46,22 +47,28 @@ pub trait PomatezTray {
}

impl PomatezTray for App {

/*
* The icon is updated after rendering on the frontend so that is handled in the commands file.
* However the initial setup and behavior is handled here.
*/
*/
fn set_pomatez_system_tray(&self) {
println!("Setting system tray");
// Was defined in tauri.config.json to start in v1
// That was created with an id of 1 though this gives more control

let show = MenuItemBuilder::with_id("show", "Show").build(self);
let quit = MenuItemBuilder::with_id("quit", "Quit").build(self);
let menu = MenuBuilder::new(self).items(&[&show, &quit]).build().expect("failed to build menu");
let menu = MenuBuilder::new(self)
.items(&[&show, &quit])
.build()
.expect("failed to build menu");

let icon_path = self.path().resolve::<PathBuf>("icons/icon.png".into(), BaseDirectory::Resource)
.expect("failed to resolve icon path, this should not happen as it is an internal file");
let icon_path = self
.path()
.resolve::<PathBuf>("icons/icon.png".into(), BaseDirectory::Resource)
.expect(
"failed to resolve icon path, this should not happen as it is an internal file",
);

let _ = TrayIconBuilder::new()
.menu(&menu)
Expand All @@ -80,12 +87,14 @@ impl PomatezTray for App {
.on_tray_icon_event(|tray, event| {
if event.click_type == ClickType::Left {
let app = tray.app_handle();
main_window::create_main_window(app);
let window = app.get_window("main").unwrap();
window.show().unwrap();
window.set_focus().unwrap();
}
})
.icon(Icon::File(icon_path))
.build(self).expect("failed to build tray icon");
.build(self)
.expect("failed to build tray icon");
}
}
26 changes: 6 additions & 20 deletions app/tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,9 @@
"signingIdentity": null
},
"publisher": "Roldan Montilla Jr",
"resources": [
"icons/icon.png"
],
"resources": ["icons/icon.png"],
"shortDescription": "",
"targets": [
"deb",
"appimage",
"msi",
"nsis",
"dmg",
"updater"
],
"targets": ["deb", "appimage", "msi", "nsis", "dmg", "updater"],
"windows": {
"certificateThumbprint": null,
"digestAlgorithm": "sha256",
Expand All @@ -72,14 +63,9 @@
"security": {
"csp": null
},
"windows": [
{
"fullscreen": false,
"height": 502,
"resizable": true,
"title": "pomatez",
"width": 340
}
]
"windows": []
},
"plugins": {
"updater": {}
}
}
Loading