Skip to content

Commit

Permalink
refactor(weg): move write logic to background
Browse files Browse the repository at this point in the history
  • Loading branch information
eythaann committed Dec 20, 2024
1 parent bd8b323 commit 0aef6f1
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 205 deletions.
1 change: 1 addition & 0 deletions capabilities/general.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"log:default",
"core:event:default",
"core:webview:default",
"core:path:default",
"dialog:default"
]
}
1 change: 0 additions & 1 deletion capabilities/launcher.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"QHNlZWxlbi9sYXVuY2hlcg"
],
"permissions": [
"core:path:default",
"fs:allow-write-text-file"
]
}
20 changes: 2 additions & 18 deletions capabilities/migrated.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
{
"$schema": "../gen/schemas/windows-schema.json",
"identifier": "migrated",
"description": "permissions that were migrated from v1. windows: [settings, weg, wm]",
"description": "permissions that were migrated from v1. windows: [settings, wm]",
"local": true,
"windows": [
"QHNlZWxlbi9zZXR0aW5ncw",
"QHNlZWxlbi93ZWc*",
"QHNlZWxlbi93aW5kb3ctbWFuYWdlc*"
],
"permissions": [
"core:path:default",
"core:event:default",
"core:window:default",
"core:webview:default",
"core:app:default",
"core:resources:default",
"core:menu:default",
Expand Down Expand Up @@ -42,13 +37,6 @@
]
},

"core:window:allow-show",
"core:window:allow-close",
"core:window:allow-start-dragging",
"core:window:allow-set-size",
"core:window:allow-set-position",
"core:window:allow-set-ignore-cursor-events",

"autostart:allow-enable",
"autostart:allow-disable",
"autostart:allow-is-enabled",
Expand All @@ -57,10 +45,6 @@
"dialog:allow-open",

"process:allow-restart",
"process:allow-exit",

"log:allow-log",

"shell:allow-open"
"process:allow-exit"
]
}
1 change: 0 additions & 1 deletion capabilities/toolbar.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"QHNlZWxlbi9mYW5jeS10b29sYmFy*"
],
"permissions": [
"core:path:default",
"fs:allow-write-text-file",
"fs:scope-appdata-recursive"
]
Expand Down
12 changes: 12 additions & 0 deletions capabilities/weg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "../gen/schemas/windows-schema.json",
"identifier": "@seleen/weg",
"windows": [
"QHNlZWxlbi93ZWc*"
],
"permissions": [
"fs:allow-exists",
"fs:allow-write-text-file",
"fs:scope-appdata-recursive"
]
}
15 changes: 5 additions & 10 deletions src/apps/seelenweg/modules/shared/store/storeApi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { WegItemType } from '@seelen-ui/lib';
import { WegItem, WegItems } from '@seelen-ui/lib/types';
import { path } from '@tauri-apps/api';
import { writeTextFile } from '@tauri-apps/plugin-fs';
import yaml from 'js-yaml';
import { WegItems, WegItemType } from '@seelen-ui/lib';
import { WegItem } from '@seelen-ui/lib/types';
import { debounce } from 'lodash';

import { store } from './infra';
Expand Down Expand Up @@ -34,15 +31,13 @@ export const savePinnedItems = debounce(
return acc;
};

const data: WegItems = {
const data = new WegItems({
left: state.itemsOnLeft.reduce(cb, []),
center: state.itemsOnCenter.reduce(cb, []),
right: state.itemsOnRight.reduce(cb, []),
};
});

const yaml_route = await path.join(await path.appDataDir(), 'seelenweg_items.yaml');
IsSavingPinnedItems.current = true;
await writeTextFile(yaml_route, yaml.dump(data));
await data.save();
},
1000,
);
1 change: 1 addition & 0 deletions src/background/exposed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ pub fn register_invoke_handler(app_builder: Builder<Wry>) -> Builder<Wry> {
state_get_default_settings,
state_get_default_monitor_settings,
state_write_settings,
state_write_weg_items,
state_get_specific_apps_configurations,
state_get_wallpaper,
state_set_wallpaper,
Expand Down
262 changes: 133 additions & 129 deletions src/background/seelen_weg/handler.rs
Original file line number Diff line number Diff line change
@@ -1,129 +1,133 @@
use std::{ffi::OsStr, path::PathBuf, sync::atomic::Ordering};

use image::ImageFormat;
use seelen_core::state::{PinnedWegItemData, WegItem};
use tauri::Emitter;
use tauri_plugin_shell::ShellExt;

use crate::{
error_handler::Result, hook::LAST_ACTIVE_NOT_SEELEN, seelen::get_app_handle,
state::application::FULL_STATE, trace_lock, windows_api::WindowsApi,
};
use windows::Win32::{
Foundation::HWND,
UI::WindowsAndMessaging::{SW_MINIMIZE, SW_RESTORE, WM_CLOSE},
};

use super::SeelenWeg;

#[tauri::command(async)]
pub fn weg_request_update_previews(handles: Vec<isize>) -> Result<()> {
let temp_dir = std::env::temp_dir();

for addr in handles {
let hwnd: HWND = HWND(addr as _);

if hwnd.is_invalid() || !WindowsApi::is_window_visible(hwnd) {
SeelenWeg::remove_hwnd(hwnd);
continue;
}

if WindowsApi::is_iconic(hwnd) {
continue;
}

let image = SeelenWeg::capture_window(hwnd);
if let Some(image) = image {
let rect = WindowsApi::get_inner_window_rect(hwnd)?;
let shadow = WindowsApi::shadow_rect(hwnd)?;
let width = rect.right - rect.left;
let height = rect.bottom - rect.top;

let image = image.crop_imm(
shadow.left.unsigned_abs(),
shadow.top.unsigned_abs(),
width as u32,
height as u32,
);

image.save_with_format(temp_dir.join(format!("{}.png", addr)), ImageFormat::Png)?;
get_app_handle().emit(format!("weg-preview-update-{}", addr).as_str(), ())?;
}
}
Ok(())
}

#[tauri::command(async)]
pub fn weg_close_app(hwnd: isize) -> Result<()> {
let hwnd = HWND(hwnd as _);
if !WindowsApi::is_window_visible(hwnd) {
SeelenWeg::remove_hwnd(hwnd);
} else {
WindowsApi::post_message(hwnd, WM_CLOSE, 0, 0)?;
}
Ok(())
}

#[tauri::command(async)]
pub fn weg_kill_app(hwnd: isize) -> Result<()> {
let hwnd = HWND(hwnd as _);
if !WindowsApi::is_window_visible(hwnd) {
SeelenWeg::remove_hwnd(hwnd);
} else {
let (pid, _) = WindowsApi::window_thread_process_id(hwnd);
get_app_handle()
.shell()
.command("taskkill.exe")
.args(["/F", "/PID", &pid.to_string()])
.spawn()?;
}
Ok(())
}

#[tauri::command(async)]
pub fn weg_toggle_window_state(hwnd: isize) -> Result<()> {
let hwnd = HWND(hwnd as _);

if hwnd.is_invalid() || !WindowsApi::is_window_visible(hwnd) {
SeelenWeg::remove_hwnd(hwnd);
return Ok(());
}

if WindowsApi::is_iconic(hwnd) {
WindowsApi::show_window_async(hwnd, SW_RESTORE)?;
return Ok(());
}

let last_active = LAST_ACTIVE_NOT_SEELEN.load(Ordering::Acquire);
if last_active == hwnd.0 as isize {
WindowsApi::show_window_async(hwnd, SW_MINIMIZE)?;
} else {
WindowsApi::set_foreground(hwnd)?;
}

Ok(())
}

#[tauri::command(async)]
pub fn weg_pin_item(path: PathBuf) -> Result<()> {
// todo add support to UWP for seelen rofi
let mut data = PinnedWegItemData {
path: path.clone(),
is_dir: path.is_dir(),
execution_command: path.to_string_lossy().to_string(),
};

if path.extension() == Some(OsStr::new("lnk")) {
let (program, _arguments) = WindowsApi::resolve_lnk_target(&path)?;
data.is_dir = program.is_dir();
data.execution_command = program.to_string_lossy().to_string();
}

let state = FULL_STATE.load();
let mut weg_items = trace_lock!(state.weg_items);
weg_items.center.insert(0, WegItem::Pinned(data));
weg_items.sanitize();
state.emit_weg_items(&weg_items)?;
state.save_weg_items(&weg_items)?;
Ok(())
}
use std::{ffi::OsStr, path::PathBuf, sync::atomic::Ordering};

use image::ImageFormat;
use seelen_core::state::{PinnedWegItemData, WegItem};
use tauri::Emitter;
use tauri_plugin_shell::ShellExt;

use crate::{
error_handler::Result, hook::LAST_ACTIVE_NOT_SEELEN, seelen::get_app_handle,
state::application::FULL_STATE, windows_api::WindowsApi,
};
use windows::Win32::{
Foundation::HWND,
UI::WindowsAndMessaging::{SW_MINIMIZE, SW_RESTORE, WM_CLOSE},
};

use super::SeelenWeg;

#[tauri::command(async)]
pub fn weg_request_update_previews(handles: Vec<isize>) -> Result<()> {
let temp_dir = std::env::temp_dir();

for addr in handles {
let hwnd: HWND = HWND(addr as _);

if hwnd.is_invalid() || !WindowsApi::is_window_visible(hwnd) {
SeelenWeg::remove_hwnd(hwnd);
continue;
}

if WindowsApi::is_iconic(hwnd) {
continue;
}

let image = SeelenWeg::capture_window(hwnd);
if let Some(image) = image {
let rect = WindowsApi::get_inner_window_rect(hwnd)?;
let shadow = WindowsApi::shadow_rect(hwnd)?;
let width = rect.right - rect.left;
let height = rect.bottom - rect.top;

let image = image.crop_imm(
shadow.left.unsigned_abs(),
shadow.top.unsigned_abs(),
width as u32,
height as u32,
);

image.save_with_format(temp_dir.join(format!("{}.png", addr)), ImageFormat::Png)?;
get_app_handle().emit(format!("weg-preview-update-{}", addr).as_str(), ())?;
}
}
Ok(())
}

#[tauri::command(async)]
pub fn weg_close_app(hwnd: isize) -> Result<()> {
let hwnd = HWND(hwnd as _);
if !WindowsApi::is_window_visible(hwnd) {
SeelenWeg::remove_hwnd(hwnd);
} else {
WindowsApi::post_message(hwnd, WM_CLOSE, 0, 0)?;
}
Ok(())
}

#[tauri::command(async)]
pub fn weg_kill_app(hwnd: isize) -> Result<()> {
let hwnd = HWND(hwnd as _);
if !WindowsApi::is_window_visible(hwnd) {
SeelenWeg::remove_hwnd(hwnd);
} else {
let (pid, _) = WindowsApi::window_thread_process_id(hwnd);
get_app_handle()
.shell()
.command("taskkill.exe")
.args(["/F", "/PID", &pid.to_string()])
.spawn()?;
}
Ok(())
}

#[tauri::command(async)]
pub fn weg_toggle_window_state(hwnd: isize) -> Result<()> {
let hwnd = HWND(hwnd as _);

if hwnd.is_invalid() || !WindowsApi::is_window_visible(hwnd) {
SeelenWeg::remove_hwnd(hwnd);
return Ok(());
}

if WindowsApi::is_iconic(hwnd) {
WindowsApi::show_window_async(hwnd, SW_RESTORE)?;
return Ok(());
}

let last_active = LAST_ACTIVE_NOT_SEELEN.load(Ordering::Acquire);
if last_active == hwnd.0 as isize {
WindowsApi::show_window_async(hwnd, SW_MINIMIZE)?;
} else {
WindowsApi::set_foreground(hwnd)?;
}

Ok(())
}

#[tauri::command(async)]
pub fn weg_pin_item(path: PathBuf) -> Result<()> {
// todo add support to UWP for seelen rofi
let mut data = PinnedWegItemData {
path: path.clone(),
is_dir: path.is_dir(),
execution_command: path.to_string_lossy().to_string(),
};

if path.extension() == Some(OsStr::new("lnk")) {
let (program, _arguments) = WindowsApi::resolve_lnk_target(&path)?;
data.is_dir = program.is_dir();
data.execution_command = program.to_string_lossy().to_string();
}

FULL_STATE.rcu(move |state| {
let mut state = state.cloned();
state
.weg_items
.center
.insert(0, WegItem::Pinned(data.clone()));
state.weg_items.sanitize();
state
});
FULL_STATE.load().write_weg_items()?;
Ok(())
}
Loading

0 comments on commit 0aef6f1

Please sign in to comment.