Skip to content

Commit

Permalink
feat: single instance
Browse files Browse the repository at this point in the history
Refactor window control into own mod
  • Loading branch information
mrjackwills committed Oct 7, 2023
1 parent 5aac721 commit e742c5d
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 82 deletions.
4 changes: 2 additions & 2 deletions .eslintrc-auto-import.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"toRaw": true,
"toRef": true,
"toRefs": true,
"toValue": true,
"triggerRef": true,
"unref": true,
"useAttrs": true,
Expand All @@ -67,7 +68,6 @@
"watch": true,
"watchEffect": true,
"watchPostEffect": true,
"watchSyncEffect": true,
"toValue": true
"watchSyncEffect": true
}
}
158 changes: 81 additions & 77 deletions src-tauri/src/internal_message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
request_handlers::{EmitMessages, ShowTimer},
system_tray::{menu_enabled, MenuItem},
tick::tick_process,
ObliqoroWindow,
ObliqoroWindow, window_action::WindowAction,
};
use tokio::sync::broadcast::{Receiver, Sender};

Expand Down Expand Up @@ -58,6 +58,7 @@ pub enum WindowVisibility {
Hide,
Minimize,
Toggle,
Show,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Hash)]
Expand Down Expand Up @@ -85,72 +86,79 @@ pub enum InternalMessage {
}

/// Control the frontend window component visibility
struct WindowAction;
impl WindowAction {
/// Show the window
/// Linux v Windows, need to handle fullscreen & resize on each platform differently
#[cfg(target_os = "windows")]
fn show(window: &tauri::Window, fullscreen: bool) {
window.set_fullscreen(fullscreen).ok();
window.set_resizable(false).ok();
window.show().ok();
window.center().ok();
}

/// Show the window
/// see github issue #1
#[cfg(not(target_os = "windows"))]
fn show(window: &tauri::Window, fullscreen: bool) {
if fullscreen {
if window.is_visible().unwrap_or_default() {
window.hide().ok();
}
window.set_resizable(true).ok();
window.set_fullscreen(fullscreen).ok();
// This is the linux fix - dirty, but it seems to work
std::thread::sleep(std::time::Duration::from_millis(50));
} else if window.is_resizable().unwrap_or(false) {
window.set_resizable(false).ok();
}
window.show().ok();
window.center().ok();
}

/// Hide window
fn hide(window: &tauri::Window, fullscreen: bool) {
if fullscreen {
window.set_resizable(true).ok();
window.set_fullscreen(false).ok();
}
window.hide().ok();
window.center().ok();
}

/// hide window
pub fn hide_window(app: &AppHandle, fullscreen: bool) {
if let Some(window) = app.get_window(ObliqoroWindow::Main.as_str()) {
Self::hide(&window, fullscreen);
}
}

/// Toggle the visible of the main window based on current visibility
pub fn toggle_visibility(app: &AppHandle, fullscreen: bool) {
if let Some(window) = app.get_window(ObliqoroWindow::Main.as_str()) {
match window.is_visible() {
Ok(true) => Self::hide(&window, fullscreen),
Ok(false) => Self::show(&window, fullscreen),
Err(_) => app.exit(1),
}
}
}

// unminimize the main window
// pub fn unminimize(app: &AppHandle) {
// if let Some(window) = app.get_window(ObliqoroWindow::Main.as_str()) {
// window.unminimize().unwrap_or_default();
// }
// }
}
// struct WindowAction;
// impl WindowAction {
// /// Show the window
// /// Linux v Windows, need to handle fullscreen & resize on each platform differently
// #[cfg(target_os = "windows")]
// fn show(window: &tauri::Window, fullscreen: bool) {
// window.set_fullscreen(fullscreen).ok();
// window.set_resizable(false).ok();
// window.show().ok();
// window.center().ok();
// }

// /// Show the window
// /// see github issue #1
// #[cfg(not(target_os = "windows"))]
// fn show(window: &tauri::Window, fullscreen: bool) {
// if fullscreen {
// if window.is_visible().unwrap_or_default() {
// window.hide().ok();
// }
// window.set_resizable(true).ok();
// window.set_fullscreen(fullscreen).ok();
// // This is the linux fix - dirty, but it seems to work
// std::thread::sleep(std::time::Duration::from_millis(50));
// } else if window.is_resizable().unwrap_or(false) {
// window.set_resizable(false).ok();
// }
// window.show().ok();
// window.center().ok();
// }

// /// Hide window
// fn hide(window: &tauri::Window, fullscreen: bool) {
// if fullscreen {
// window.set_resizable(true).ok();
// window.set_fullscreen(false).ok();
// }
// window.hide().ok();
// window.center().ok();
// }

// /// show window
// pub fn show_window(app: &AppHandle, fullscreen: bool) {
// if let Some(window) = app.get_window(ObliqoroWindow::Main.as_str()) {
// Self::show(&window, fullscreen);
// }
// }

// /// hide window
// pub fn hide_window(app: &AppHandle, fullscreen: bool) {
// if let Some(window) = app.get_window(ObliqoroWindow::Main.as_str()) {
// Self::hide(&window, fullscreen);
// }
// }

// /// Toggle the visible of the main window based on current visibility
// pub fn toggle_visibility(app: &AppHandle, fullscreen: bool) {
// if let Some(window) = app.get_window(ObliqoroWindow::Main.as_str()) {
// match window.is_visible() {
// Ok(true) => Self::hide(&window, fullscreen),
// Ok(false) => Self::show(&window, fullscreen),
// Err(_) => app.exit(1),
// }
// }
// }

// // unminimize the main window
// // pub fn unminimize(app: &AppHandle) {
// // if let Some(window) = app.get_window(ObliqoroWindow::Main.as_str()) {
// // window.unminimize().unwrap_or_default();
// // }
// // }
// }

// Update the taskbar to display how many sessions before next long break,
// and send internal message, to send message to front end to update settings in pinia
Expand Down Expand Up @@ -311,12 +319,12 @@ fn handle_visibility(
WindowAction::hide_window(app, false);
}
}

WindowVisibility::Minimize => {
// if on_break {
// WindowAction::unminimize(app);
// } else {
WindowAction::hide_window(app, false);
// }
}
WindowVisibility::Show => {
WindowAction::show_window(app, false);
}
WindowVisibility::Toggle => {
if !on_break {
Expand Down Expand Up @@ -440,16 +448,12 @@ fn handle_break(
state.lock().start_break_session();
menu_enabled(app, false);
sx.send(InternalMessage::Emit(Emitter::Timer)).ok();
if let Some(window) = app.get_window(ObliqoroWindow::Main.as_str()) {
WindowAction::show(&window, fullscreen);
}
WindowAction::show_window(app, fullscreen);
}
BreakMessage::End => {
state.lock().start_work_session();
menu_enabled(app, true);
if let Some(window) = app.get_window(ObliqoroWindow::Main.as_str()) {
WindowAction::hide(&window, fullscreen);
}
WindowAction::hide_window(app, fullscreen);
update_menu(app, state, sx);
}
}
Expand Down
9 changes: 7 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use tick::tick_process;
use tracing::{error, Level};
use tracing_subscriber::{fmt as t_fmt, prelude::__tracing_subscriber_SubscriberExt};

#[allow(unused_imports)]
#[cfg(debug_assertions)]
use tauri::Manager;

mod app_error;
Expand All @@ -34,6 +34,7 @@ mod internal_message_handler;
mod request_handlers;
mod system_tray;
mod tick;
mod window_action;

pub type TauriState<'a> = tauri::State<'a, Arc<Mutex<ApplicationState>>>;

Expand Down Expand Up @@ -69,7 +70,7 @@ fn setup_tracing(app_dir: &PathBuf) -> Result<(), AppError> {
.finish()
.with(log_fmt),
) {
Ok(_) => Ok(()),
Ok(()) => Ok(()),
Err(e) => {
println!("{e:?}");
Err(AppError::Internal("Unable to start tracing".to_owned()))
Expand Down Expand Up @@ -119,6 +120,7 @@ async fn main() -> Result<(), ()> {

let event_sx = sx.clone();
let close_sx = sx.clone();
let instance_sx = sx.clone();
let handler_sx = sx.clone();
let tray_sx = sx.clone();

Expand Down Expand Up @@ -168,6 +170,9 @@ async fn main() -> Result<(), ()> {
request_handlers::set_setting_shortbreak,
request_handlers::toggle_pause,
])
.plugin(tauri_plugin_single_instance::init(move |app, argv, cwd| {
instance_sx.send(InternalMessage::Window(WindowVisibility::Show)).ok();
}))
.build(tauri::generate_context!())
{
Ok(s) => {
Expand Down
78 changes: 78 additions & 0 deletions src-tauri/src/window_action.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use tauri::{AppHandle, Manager};

use crate::ObliqoroWindow;

pub struct WindowAction;

impl WindowAction {
/// Show the window
/// Linux v Windows, need to handle fullscreen & resize on each platform differently
#[cfg(target_os = "windows")]
fn show(window: &tauri::Window, fullscreen: bool) {
window.set_fullscreen(fullscreen).ok();
window.set_resizable(false).ok();
window.show().ok();
window.center().ok();
}

/// Show the window
/// see github issue #1
#[cfg(not(target_os = "windows"))]
fn show(window: &tauri::Window, fullscreen: bool) {
if fullscreen {
if window.is_visible().unwrap_or_default() {
window.hide().ok();
}
window.set_resizable(true).ok();
window.set_fullscreen(fullscreen).ok();
// This is the linux fix - dirty, but it seems to work
std::thread::sleep(std::time::Duration::from_millis(50));
} else if window.is_resizable().unwrap_or(false) {
window.set_resizable(false).ok();
}
window.show().ok();
window.center().ok();
}

/// Hide window
fn hide(window: &tauri::Window, fullscreen: bool) {
if fullscreen {
window.set_resizable(true).ok();
window.set_fullscreen(false).ok();
}
window.hide().ok();
window.center().ok();
}

/// show window
pub fn show_window(app: &AppHandle, fullscreen: bool) {
if let Some(window) = app.get_window(ObliqoroWindow::Main.as_str()) {
Self::show(&window, fullscreen);
}
}

/// hide window
pub fn hide_window(app: &AppHandle, fullscreen: bool) {
if let Some(window) = app.get_window(ObliqoroWindow::Main.as_str()) {
Self::hide(&window, fullscreen);
}
}

/// Toggle the visible of the main window based on current visibility
pub fn toggle_visibility(app: &AppHandle, fullscreen: bool) {
if let Some(window) = app.get_window(ObliqoroWindow::Main.as_str()) {
match window.is_visible() {
Ok(true) => Self::hide(&window, fullscreen),
Ok(false) => Self::show(&window, fullscreen),
Err(_) => app.exit(1),
}
}
}

// unminimize the main window
// pub fn unminimize(app: &AppHandle) {
// if let Some(window) = app.get_window(ObliqoroWindow::Main.as_str()) {
// window.unminimize().unwrap_or_default();
// }
// }
}
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"resources": [],
"shortDescription": "",
"targets": ["deb", "msi", "app", "dmg", "updater"],
"targets": ["deb", "msi", "app", "appimage", "dmg", "updater"],
"windows": {
"certificateThumbprint": null,
"digestAlgorithm": "sha256",
Expand Down

0 comments on commit e742c5d

Please sign in to comment.