Skip to content

Commit

Permalink
feat: start with system and minimized capability
Browse files Browse the repository at this point in the history
Fixes: #141
  • Loading branch information
Xstoudi committed Jul 20, 2023
1 parent e9aef42 commit 768f81c
Show file tree
Hide file tree
Showing 11 changed files with 406 additions and 257 deletions.
548 changes: 315 additions & 233 deletions src-tauri/Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ edition = "2021"
tauri-build = { version = "1.4", features = [] }

[dependencies]
tauri = { version = "1.4", features = [ "updater", "system-tray", "path-all", "fs-all", "window-start-dragging", "window-maximize", "window-hide", "window-close", "window-unminimize", "window-show", "window-unmaximize", "shell-open", "window-minimize"] }
tauri = { version = "1.4", features = [ "cli", "updater", "system-tray", "path-all", "fs-all", "window-start-dragging", "window-maximize", "window-hide", "window-close", "window-unminimize", "window-show", "window-unmaximize", "shell-open", "window-minimize"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
reqwest = { version = "0.11", features = ["blocking", "json"] }
chrono = "0.4.26"
feed-rs = "1.3.0"
tauri-plugin-window-state = "0.1.0"
tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tauri-plugin-autostart = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tauri-plugin-sql = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1", features = ["sqlite"] }

Expand Down
3 changes: 3 additions & 0 deletions src-tauri/src/commands/splashscreen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ pub async fn close_splashscreen(window: Window) {
if let Some(splashscreen) = window.get_window("splashscreen") {
splashscreen.close().unwrap();
}
}

#[tauri::command]
pub async fn open_main_window(window: Window) {
window.get_window("main").unwrap().show().unwrap();
}
14 changes: 10 additions & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ pub mod structs;
pub mod enums;

use commands::fetcher::{sync, sync_all};
use commands::splashscreen::close_splashscreen;
use commands::splashscreen::{close_splashscreen, open_main_window};
use tauri::{generate_handler, generate_context, Manager, Builder, SystemTray, SystemTrayEvent, SystemTrayMenu, CustomMenuItem, AppHandle, Wry};
use tauri_plugin_autostart::MacosLauncher;
use tauri_plugin_window_state::StateFlags;

fn show_main_window(app: &AppHandle<Wry>) {
let window = app.get_window("main").unwrap();
Expand All @@ -30,11 +31,16 @@ fn main() {
let system_tray = SystemTray::new()
.with_menu(tray_menu);

let mut flags = StateFlags::all();
flags.remove(StateFlags::VISIBLE);

Builder::default()
.plugin(tauri_plugin_window_state::Builder::default().build())
.plugin(tauri_plugin_window_state::Builder::default()
.with_state_flags(flags)
.build())
.plugin(tauri_plugin_sql::Builder::default().build())
.plugin(tauri_plugin_autostart::init(MacosLauncher::LaunchAgent, None))
.invoke_handler(generate_handler![sync, sync_all, close_splashscreen])
.plugin(tauri_plugin_autostart::init(MacosLauncher::LaunchAgent, Some(vec!["--autostart"])))
.invoke_handler(generate_handler![sync, sync_all, close_splashscreen, open_main_window])
.system_tray(system_tray)
.on_system_tray_event(|app, event| match event {
SystemTrayEvent::DoubleClick {
Expand Down
26 changes: 17 additions & 9 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@
"csp": null
},
"windows": [
{
"width": 800,
"height": 800,
"decorations": false,
"url": "splashscreen.html",
"label": "splashscreen",
"center": true,
"transparent": true
},
{
"fullscreen": false,
"resizable": true,
Expand All @@ -71,15 +80,6 @@
"minHeight": 200,
"decorations": false,
"visible": false
},
{
"width": 800,
"height": 800,
"decorations": false,
"url": "splashscreen.html",
"label": "splashscreen",
"center": true,
"transparent": true
}
],
"systemTray": {
Expand All @@ -93,6 +93,14 @@
],
"dialog": true,
"pubkey": "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IDNEQUQ2MTY4NERDRTc0QUIKUldTcmRNNU5hR0d0UFphQ0Y0ZXBhVG9IRWsxcFNwUTlMWjBoKytkUld1ZFpFcnBOZFNWRHh5bmUK"
},
"cli": {
"args": [
{
"name": "autostart",
"short": "a"
}
]
}
}
}
10 changes: 9 additions & 1 deletion src/components/form/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ interface SwitchProps {
label: string;
value: boolean;
onChange: (value: boolean) => void;
disabled?: boolean;
}

function Switch({ name, label, value, onChange }: SwitchProps) {
function Switch({
name,
label,
value,
onChange,
disabled = false,
}: SwitchProps) {
return (
<Form.Field name={name} className="grid mb-2">
<div className="flex items-baseline justify-between">
Expand All @@ -19,6 +26,7 @@ function Switch({ name, label, value, onChange }: SwitchProps) {
className="w-[42px] h-[25px] bg-black rounded-full relative outline-none cursor-pointer"
checked={value}
onCheckedChange={onChange}
disabled={disabled}
>
<RadixSwitch.Thumb className="block w-[21px] h-[21px] bg-white rounded-full transition-transform duration-100 translate-x-0.5 will-change-transform data-[state=checked]:translate-x-[19px]" />
</RadixSwitch.Root>
Expand Down
31 changes: 25 additions & 6 deletions src/components/modal/PreferenceModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function PreferenceModal() {
showFeedIcons: false,
showArticleThumbnails: false,
autoStart: false,
startMinimized: false,
}
: state,
[isStateEmpty, state],
Expand Down Expand Up @@ -94,12 +95,30 @@ function PreferenceModal() {
setForm({ ...form, showArticleThumbnails })
}
/>
<Switch
label="Launch at startup"
name="autoStart"
value={form.autoStart}
onChange={(autoStart) => setForm({ ...form, autoStart })}
/>
<div className="flex flex-row gap-8">
<Switch
label="Launch at startup"
name="autoStart"
value={form.autoStart}
onChange={(autoStart) =>
setForm({
...form,
autoStart,
startMinimized: !autoStart ? false : form.startMinimized,
})
}
/>
{form.autoStart && (
<Switch
label="Start minimized"
name="startMinimized"
value={form.startMinimized}
onChange={(startMinimized) =>
setForm({ ...form, startMinimized })
}
/>
)}
</div>

<div className={clsx('mt-4 flex justify-between flex-row-reverse')}>
<Form.Submit asChild>
Expand Down
23 changes: 21 additions & 2 deletions src/components/utils/LoadManager.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import { invoke } from '@tauri-apps/api';
import { getMatches } from '@tauri-apps/api/cli';
import { memo, useEffect } from 'react';
import { useToggle } from 'react-use';

import usePreference from '../../hooks/usePreference';

import AutostartManager from './AutostartManager';
import BackupManager from './BackupManager';

function LoadManager() {
const [backupLoaded, toggleBackupLoaded] = useToggle(false);
const [autostartLoaded, toggleAutostartLoaded] = useToggle(false);
const { startMinimized } = usePreference();

useEffect(() => {
if (!backupLoaded || !autostartLoaded) return;
invoke('close_splashscreen').catch((error) => console.error(error));
}, [autostartLoaded, backupLoaded]);
async function bootedRoutine() {
const matches = await getMatches();

await invoke('close_splashscreen');

const autostarting = (matches.args?.autostart?.occurrences ?? 0) > 0;
console.log(matches, autostarting, startMinimized);

if (autostarting && startMinimized) {
return;
}

await invoke('open_main_window');
}

bootedRoutine().catch(console.error);
}, [autostartLoaded, backupLoaded, startMinimized]);

return (
<>
Expand Down
1 change: 1 addition & 0 deletions src/data/transformers/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default {
showFeedIcons: true,
showArticleThumbnails: true,
autoStart: true,
startMinimized: false,
},
};
},
Expand Down
2 changes: 2 additions & 0 deletions src/state/preference/PreferenceReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ export interface PreferenceState {
showFeedIcons: boolean;
showArticleThumbnails: boolean;
autoStart: boolean;
startMinimized: boolean;
}

export const initialPreferenceState: PreferenceState = {
darkMode: true,
showFeedIcons: true,
showArticleThumbnails: true,
autoStart: true,
startMinimized: false,
};

export type PreferenceActions = SetPreferencesAction;
Expand Down
1 change: 1 addition & 0 deletions src/state/preference/actions/GeneralActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export function setPreferences(
draft.showFeedIcons = payload.showFeedIcons;
draft.showArticleThumbnails = payload.showArticleThumbnails;
draft.autoStart = payload.autoStart;
draft.startMinimized = payload.startMinimized;
}

0 comments on commit 768f81c

Please sign in to comment.