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

feat: support startup screen #6

Merged
merged 3 commits into from
Nov 24, 2024
Merged
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
2 changes: 1 addition & 1 deletion index.html → main.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
<script type="module" src="./src/pages/main/main.ts"></script>
</body>
</html>
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@tauri-apps/plugin-dialog": "~2.0.1",
"@tauri-apps/plugin-fs": "~2.0.2",
"@tauri-apps/plugin-global-shortcut": "~2",
"@tauri-apps/plugin-os": "~2",
"@tauri-apps/plugin-shell": "^2.0.1",
"@tauri-apps/plugin-store": "~2",
"@vueuse/core": "^11.2.0",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 54 additions & 3 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ tauri-plugin-store = "2"
xcap = "0.0.14"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tracing = "0.1.40"
tauri-plugin-os = "2"
core-foundation = "0.10.0"
libc = "0.2.164"

[features]
default = ["custom-protocol"]
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/capabilities/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "Capability for the main window",
"windows": ["main", "setting", "preview"],
"windows": ["main", "setting", "preview", "startup"],
"permissions": [
"core:default",
"shell:allow-open",
Expand Down
16 changes: 16 additions & 0 deletions src-tauri/capabilities/os.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"identifier": "os-capability",
"description": "Allow access os to window",
"platforms": [
"macOS",
"windows",
"linux"
],
"windows": [
"startup"
],
"permissions": [
"os:default",
"os:allow-version"
]
}
10 changes: 10 additions & 0 deletions src-tauri/src/cmd/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ pub async fn capture_window(app_handle: tauri::AppHandle, path: String) -> Resul
let filename = platform::capture_window(&app_handle, path).await?;
Ok(filename)
}

#[tauri::command]
pub fn open_screen_capture_preferences() {
platform::open_screen_capture_preferences();
}

#[tauri::command]
pub fn check_accessibility_permissions() -> bool {
platform::check_accessibility_permissions()
}
2 changes: 2 additions & 0 deletions src-tauri/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ pub const MAIN_WINDOW: &str = "main";
pub const PREVIEW_WINDOW: &str = "preview";

pub const SETTING_WINDOW: &str = "setting";

pub const STARTUP_WINDOW: &str = "startup";
6 changes: 5 additions & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ mod global_shortcut;
mod menu;
mod platform;
mod window;
mod xcap_utils;

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_os::init())
.setup(|app| {
#[cfg(desktop)]
configure_autostart(app);

window::show_startup_window(&app.handle());

#[cfg(desktop)]
let _ = global_shortcut::register_global_shortcut(app);

Expand Down Expand Up @@ -57,6 +59,8 @@ pub fn run() {
cmd::hide_main_window,
cmd::show_setting_window,
cmd::hide_setting_window,
cmd::open_screen_capture_preferences,
cmd::check_accessibility_permissions,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
30 changes: 30 additions & 0 deletions src-tauri/src/platform/mac/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::{
};

use chrono::Local;
use core_foundation::{base::TCFType, boolean::CFBoolean, string::CFString};

use tracing::info;

use crate::common::get_images_dir;
Expand Down Expand Up @@ -89,3 +91,31 @@ pub async fn capture_window(app_handle: &tauri::AppHandle, path: String) -> Resu

Ok(filename)
}

pub fn open_screen_capture_preferences() {
Command::new("open")
.arg("x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture")
.spawn()
.expect("failed to open system preferences");
}

pub fn check_accessibility_permissions() -> bool {
let options = {
let key = CFString::new("AXTrustedCheckOptionPrompt");
let value = CFBoolean::false_value();
let pairs = &[(key, value)];
core_foundation::dictionary::CFDictionary::from_CFType_pairs(pairs)
};

let trusted = unsafe {
let accessibility = CFString::new("AXIsProcessTrustedWithOptions");
let func: extern "C" fn(*const core_foundation::dictionary::CFDictionary) -> bool =
std::mem::transmute(libc::dlsym(
libc::RTLD_DEFAULT,
accessibility.to_string().as_ptr() as *const _,
));
func(options.as_concrete_TypeRef() as *const _)
};

trusted
}
11 changes: 11 additions & 0 deletions src-tauri/src/platform/mac/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,14 @@ pub fn show_setting_window(window: &WebviewWindow) {
pub fn hide_setting_window(window: &WebviewWindow) {
let _ = window.minimize();
}

pub fn show_startup_window(window: &WebviewWindow) {
let _ = window.show();
window::center_position(window);
let _ = window.set_focus();
let _ = window.unminimize();
}

pub fn hide_startup_window(window: &WebviewWindow) {
let _ = window.minimize();
}
Loading