Skip to content

Commit

Permalink
refactor(tb): make toolbar more modular (part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
eythaann committed Nov 20, 2024
1 parent 4e640fd commit 70681a6
Show file tree
Hide file tree
Showing 51 changed files with 552 additions and 240 deletions.
2 changes: 1 addition & 1 deletion capabilities/launcher.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "../gen/schemas/windows-schema.json",
"identifier": "launcher",
"windows": [
"seelen-launcher"
"seelen/launcher"
],
"permissions": [
"log:default",
Expand Down
8 changes: 2 additions & 6 deletions capabilities/migrated.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
"local": true,
"windows": [
"settings",
"seelenweg/*",
"updater",
"window-manager/*",
"fancy-toolbar/*"
"seelen/weg*",
"seelen/window-manager*"
],
"permissions": [
"core:path:default",
Expand Down Expand Up @@ -63,8 +61,6 @@

"log:allow-log",

"updater:default",

"shell:allow-open"
]
}
19 changes: 19 additions & 0 deletions capabilities/toolbar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "../gen/schemas/windows-schema.json",
"identifier": "toolbar",
"windows": [
"seelen/fancy-toolbar*"
],
"permissions": [
"log:default",
"core:path:default",
"core:webview:default",
"core:event:default",

"core:window:default",
"core:window:allow-show",
"core:window:allow-set-ignore-cursor-events",

"dialog:default"
]
}
2 changes: 1 addition & 1 deletion capabilities/wall.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "permissions for wall",
"local": true,
"windows": [
"seelen-wall"
"seelen/wall"
],
"permissions": [
"log:default",
Expand Down
16 changes: 13 additions & 3 deletions documentation/schemas/placeholder.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"default": [],
"type": "array",
"items": {
"$ref": "#/definitions/ToolbarItem"
"$ref": "#/definitions/ToolbarItem2"
}
},
"info": {
Expand All @@ -30,15 +30,15 @@
"default": [],
"type": "array",
"items": {
"$ref": "#/definitions/ToolbarItem"
"$ref": "#/definitions/ToolbarItem2"
}
},
"right": {
"description": "Items to be displayed in the toolbar",
"default": [],
"type": "array",
"items": {
"$ref": "#/definitions/ToolbarItem"
"$ref": "#/definitions/ToolbarItem2"
}
}
},
Expand Down Expand Up @@ -873,6 +873,16 @@
}
]
},
"ToolbarItem2": {
"anyOf": [
{
"type": "string"
},
{
"$ref": "#/definitions/ToolbarItem"
}
]
},
"WorkspaceToolbarItemMode": {
"type": "string",
"enum": [
Expand Down
2 changes: 2 additions & 0 deletions lib/src/handlers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ export enum SeelenEvent {
StateSettingsByAppChanged = 'settings-by-app',
StateHistoryChanged = 'history',
StateIconPacksChanged = 'icon-packs',
StatePluginsChanged = 'plugins-changed',
StateWidgetsChanged = 'widgets-changed',
}
5 changes: 5 additions & 0 deletions lib/src/handlers/invokers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { invoke as tauriInvoke, InvokeArgs, InvokeOptions } from '@tauri-apps/api/core';

import { Plugin } from '../state/plugin';

export enum SeelenCommand {
// General
Run = 'run',
Expand Down Expand Up @@ -31,6 +33,8 @@ export enum SeelenCommand {
StateGetWallpaper = 'state_get_wallpaper',
StateSetWallpaper = 'state_set_wallpaper',
StateGetHistory = 'state_get_history',
StateGetPlugins = 'state_get_plugins',
StateGetWidgets = 'state_get_widgets',

// Media
MediaPrev = 'media_prev',
Expand Down Expand Up @@ -83,6 +87,7 @@ export enum SeelenCommand {
type ReturnTypeByCommand = Record<SeelenCommand, unknown> & {
[SeelenCommand.CheckForUpdates]: boolean;
[SeelenCommand.InstallLastAvailableUpdate]: never;
[SeelenCommand.StateGetPlugins]: Plugin[];
};

export type SeelenCommandReturn<T extends SeelenCommand> = ReturnTypeByCommand[T];
Expand Down
2 changes: 2 additions & 0 deletions lib/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,6 @@ slu_events_declaration! {
StateSettingsByAppChanged = "settings-by-app",
StateHistoryChanged = "history",
StateIconPacksChanged = "icon-packs",
StatePluginsChanged = "plugins-changed",
StateWidgetsChanged = "widgets-changed",
}
1 change: 1 addition & 0 deletions lib/src/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './placeholder';
export * from './settings_by_app';
export * from './settings_by_monitor';
export * from './icon_pack';
export * from './plugin';

export interface LauncherHistory {
[x: string]: string[];
Expand Down
4 changes: 4 additions & 0 deletions lib/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ mod settings_by_monitor;
mod theme;
mod weg_items;
mod wm_layout;
mod plugin;
mod widget;

pub use icon_pack::*;
pub use placeholder::*;
Expand All @@ -15,6 +17,8 @@ pub use settings_by_monitor::*;
pub use theme::*;
pub use weg_items::*;
pub use wm_layout::*;
pub use plugin::*;
pub use widget::*;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand Down
39 changes: 28 additions & 11 deletions lib/src/state/placeholder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,13 @@ impl ToolbarItem {
}
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum ToolbarItem2 {
PluginId(String),
Inline(ToolbarItem)
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[serde(default, rename_all = "camelCase")]
pub struct PlaceholderInfo {
Expand All @@ -334,23 +341,33 @@ pub struct Placeholder {
/// Metadata about the placeholder
pub info: PlaceholderInfo,
/// Items to be displayed in the toolbar
pub left: Vec<ToolbarItem>,
pub left: Vec<ToolbarItem2>,
/// Items to be displayed in the toolbar
pub center: Vec<ToolbarItem>,
pub center: Vec<ToolbarItem2>,
/// Items to be displayed in the toolbar
pub right: Vec<ToolbarItem>,
pub right: Vec<ToolbarItem2>,
}

impl Placeholder {
fn sanitize_items(dict: &mut HashSet<String>, items: Vec<ToolbarItem>) -> Vec<ToolbarItem> {
fn sanitize_items(dict: &mut HashSet<String>, items: Vec<ToolbarItem2>) -> Vec<ToolbarItem2> {
let mut result = Vec::new();
for mut item in items {
if item.id().is_empty() {
item.set_id(uuid::Uuid::new_v4().to_string());
}
if !dict.contains(&item.id()) {
dict.insert(item.id());
result.push(item);
for item in items {
match item {
ToolbarItem2::PluginId(id) => {
if !dict.contains(&id) {
dict.insert(id.clone());
result.push(ToolbarItem2::PluginId(id));
}
}
ToolbarItem2::Inline(mut item) => {
if item.id().is_empty() {
item.set_id(uuid::Uuid::new_v4().to_string());
}
if !dict.contains(&item.id()) {
dict.insert(item.id());
result.push(ToolbarItem2::Inline(item));
}
}
}
}
result
Expand Down
6 changes: 3 additions & 3 deletions lib/src/state/placeholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export interface CreatorInfo {

export interface Placeholder {
info: CreatorInfo;
left: ToolbarModule[];
center: ToolbarModule[];
right: ToolbarModule[];
left: (string | ToolbarModule)[];
center: (string | ToolbarModule)[];
right: (string | ToolbarModule)[];
}
8 changes: 8 additions & 0 deletions lib/src/state/plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Plugin {
pub id: String,
pub target: String,
pub plugin: serde_yaml::Value,
}
33 changes: 33 additions & 0 deletions lib/src/state/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { listen, UnlistenFn } from '@tauri-apps/api/event';

import { invoke, SeelenCommand, SeelenEvent } from '../handlers';
import { getCurrentWidget } from '../utils';

export class Plugin {
id: string = '';
target: string = '';
plugin: any = {};
}

export class PluginList {
private constructor(private inner: Plugin[]) {}

static async getAsync(): Promise<PluginList> {
return new PluginList(await invoke(SeelenCommand.StateGetPlugins));
}

static async onChange(cb: (value: PluginList) => void): Promise<UnlistenFn> {
return listen<Plugin[]>(SeelenEvent.StatePluginsChanged, (event) => {
cb(new PluginList(event.payload));
});
}

all(): Plugin[] {
return this.inner;
}

forCurrentWidget(): Plugin[] {
let target = getCurrentWidget().id;
return this.inner.filter((plugin) => plugin.target === target);
}
}
2 changes: 1 addition & 1 deletion lib/src/state/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ export interface Theme {
/** Metadata about the theme */
info: ThemeInfo;
/** Css Styles of the theme */
styles: ThemeCssByApp;
styles: ThemeCssByApp & Record<string, string>;
}
6 changes: 6 additions & 0 deletions lib/src/state/widget.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Widget {
pub id: String,
}
14 changes: 14 additions & 0 deletions lib/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getCurrentWindow } from '@tauri-apps/api/window';

export * from './hooks';
export * from './layered_hitbox';

Expand Down Expand Up @@ -46,3 +48,15 @@ export function disableWebviewShortcutsAndContextMenu() {
window.addEventListener('drop', (e) => e.preventDefault());
window.addEventListener('dragover', (e) => e.preventDefault());
}

// label schema: user/resource__query__monitor:display5
export function getCurrentWidget() {
const { label } = getCurrentWindow();
const parsedLabel = label.replace('__query__', '?').replace(':', '=');
const query = new URLSearchParams(parsedLabel);
return {
id: `@${parsedLabel.split('?')[0]}`,
label,
attachedMonitor: query.get('monitor'),
};
}
25 changes: 10 additions & 15 deletions src/apps/shared/styles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { listen } from '@tauri-apps/api/event';
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow';
import { useEffect, useState } from 'react';
import { Settings, Theme, UIColors } from 'seelen-core';
import { getCurrentWidget, Settings, Theme, UIColors } from 'seelen-core';

import { UserSettingsLoader } from '../settings/modules/shared/store/storeApi';

Expand Down Expand Up @@ -45,11 +44,11 @@ export function useDarkMode() {
}

const KeyByLabel: Record<string, string> = {
'fancy-toolbar': 'toolbar',
seelenweg: 'weg',
'window-manager': 'wm',
'seelen-launcher': 'launcher',
'seelen-wall': 'wall',
'@seelen/fancy-toolbar': 'toolbar',
'@seelen/weg': 'weg',
'@seelen/window-manager': 'wm',
'@seelen/launcher': 'launcher',
'@seelen/wall': 'wall',
};

async function loadThemes(allThemes: Theme[], selected: string[]) {
Expand All @@ -59,20 +58,16 @@ async function loadThemes(allThemes: Theme[], selected: string[]) {
return selected.indexOf(a.info.filename) - selected.indexOf(b.info.filename);
});

const webviewId = getCurrentWebviewWindow().label;
const [label, _monitor] = webviewId.split('/');
if (!label) {
return;
}
const widget = getCurrentWidget();

const theme_key = KeyByLabel[label] as keyof Theme['styles'] | undefined;
const theme_key = KeyByLabel[widget.id] || widget.id;
if (!theme_key) {
return;
}

document.getElementById(webviewId)?.remove();
document.getElementById(widget.label)?.remove();
let element = document.createElement('style');
element.id = webviewId;
element.id = widget.label;
element.textContent = '';

for (const theme of themes) {
Expand Down
Loading

0 comments on commit 70681a6

Please sign in to comment.