Skip to content

Commit

Permalink
fix toggle in front end
Browse files Browse the repository at this point in the history
  • Loading branch information
0-don committed Dec 21, 2024
1 parent afb40f0 commit 4bde986
Show file tree
Hide file tree
Showing 31 changed files with 191 additions and 131 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
node_modules
dist
clippy.sqlite
*.sqlite
yarn.lock
package-lock.json
pnpm-lock.yamlor.log
Expand All @@ -15,4 +15,5 @@ bun.lockb
# will have compiled files and executables
target/
context.txt
sea_orm_*.txt
sea_orm_*.txt
config.json
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
node_modules
dist
clippy.sqlite
*.sqlite
yarn.lock
package-lock.json
pnpm-lock.yaml
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dev": "vite",
"build": "vite build",
"tauri": "cross-env NO_STRIP=true tauri",
"d": "tauri dev",
"d": "cross-env RUST_BACKTRACE=1 tauri dev",
"gen": "sea-orm-cli migrate refresh -v -d src-tauri/migration && sea-orm-cli generate entity -l -o src-tauri/entity/src --expanded-format --with-serde both",
"icon": "tauri icon"
},
Expand Down
20 changes: 20 additions & 0 deletions src-tauri/Cargo.lock

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

2 changes: 2 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ infer = "0"
mime_guess = "2"
tree_magic_mini = "3"

color-backtrace = "0"

[profile.release]
# panic = "abort"
# codegen-units = 1
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/common/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub static GLOBAL_EVENTS: LazyLock<Vec<String>> = LazyLock::new(|| {
]
});

pub static DB_NAME: &str = "clippy.sqlite";
pub static CONFIG_NAME: &str = "config.json";

pub static MAIN_WINDOW_X: i32 = 375;
pub static MAIN_WINDOW_Y: i32 = 600;

Expand Down
3 changes: 0 additions & 3 deletions src-tauri/entity/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub struct Model {
pub id: i32,
pub language: String,
pub startup: bool,
pub notification: bool,
pub synchronize: bool,
pub dark_mode: bool,
pub display_scale: f32,
Expand All @@ -33,7 +32,6 @@ pub enum Column {
Id,
Language,
Startup,
Notification,
Synchronize,
DarkMode,
DisplayScale,
Expand Down Expand Up @@ -66,7 +64,6 @@ impl ColumnTrait for Column {
Self::Id => ColumnType::Integer.def(),
Self::Language => ColumnType::String(StringLen::N(2u32)).def(),
Self::Startup => ColumnType::Boolean.def(),
Self::Notification => ColumnType::Boolean.def(),
Self::Synchronize => ColumnType::Boolean.def(),
Self::DarkMode => ColumnType::Boolean.def(),
Self::DisplayScale => ColumnType::Float.def(),
Expand Down
4 changes: 1 addition & 3 deletions src-tauri/migration/src/m000007_create_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use common::{
};
use sea_orm_migration::{
prelude::*,
schema::{boolean, float, integer, pk_auto, string},
schema::{boolean, float, integer, pk_auto, string},
};

#[derive(Iden)]
Expand All @@ -19,7 +19,6 @@ enum Settings {
Language,
//
Startup,
Notification,
Synchronize,
DarkMode,
DisplayScale,
Expand Down Expand Up @@ -49,7 +48,6 @@ impl MigrationTrait for Migration {
.default(get_system_language().to_string()),
)
.col(boolean(Settings::Startup).default(true))
.col(boolean(Settings::Notification).default(false))
.col(boolean(Settings::Synchronize).default(false))
.col(boolean(Settings::DarkMode).default(true))
.col(
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;
use crate::service::settings::get_data_path;
use common::types::types::Config;
use common::{constants::DB_NAME, types::types::Config};
use migration::{DbErr, Migrator, MigratorTrait};
use std::sync::Once;

Expand All @@ -9,7 +9,7 @@ static INIT: Once = Once::new();

pub async fn db() -> Result<DbConn, DbErr> {
let database_url = if cfg!(debug_assertions) {
String::from("sqlite://../clippy.sqlite?mode=rwc")
format!("sqlite://../{}?mode=rwc", DB_NAME)
} else {
get_prod_database_url()
};
Expand Down
2 changes: 2 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#[tokio::main]
async fn main() {
color_backtrace::install();

#[cfg(target_os = "linux")]
{
// See: https://github.com/spacedriveapp/spacedrive/issues/1512#issuecomment-1758550164
Expand Down
15 changes: 10 additions & 5 deletions src-tauri/src/service/hotkey.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::global::{get_app_window, get_main_window};
use super::global::{get_app, get_main_window};
use crate::prelude::*;
use crate::{
connection,
Expand All @@ -8,7 +8,7 @@ use common::types::enums::{ListenEvent, WebWindow};
use core::future::Future;
use entity::hotkey::{self, ActiveModel, Model};
use sea_orm::{ActiveModelTrait, EntityTrait};
use tauri::Emitter;
use tauri::{Emitter, Manager};

pub async fn get_all_hotkeys_db() -> Result<Vec<Model>, DbErr> {
let db: DatabaseConnection = connection::db().await?;
Expand Down Expand Up @@ -45,9 +45,14 @@ where
get_main_window()
.emit(ListenEvent::Init.to_string().as_str(), ())
.expect("Failed to emit init event");
get_app_window(WebWindow::Settings)
.emit(ListenEvent::Init.to_string().as_str(), ())
.expect("Failed to emit init event");

if let Some(settings_window) =
get_app().get_webview_window(WebWindow::Settings.to_string().as_str())
{
settings_window
.emit(ListenEvent::Init.to_string().as_str(), ())
.expect("Failed to emit init event");
}

result
}
39 changes: 28 additions & 11 deletions src-tauri/src/service/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::connection;
use crate::prelude::*;
use crate::service::window::get_monitor_scale_factor;
use crate::{commands::settings::get_settings, service::hotkey::with_hotkeys};
use common::constants::CONFIG_NAME;
use common::constants::DB_NAME;
use common::language::get_system_language;
use common::types::types::Config;
use common::types::types::DataPath;
Expand Down Expand Up @@ -73,23 +75,34 @@ pub fn init_settings() {
}

pub fn get_data_path() -> DataPath {
let config_path = get_app()
.path()
.app_data_dir()
.expect("Failed to get app data dir")
.to_string_lossy()
.to_string();
let config_path = if cfg!(debug_assertions) {
// Get absolute project root directory
let current_dir = std::env::current_dir()
.expect("Failed to get current directory")
.parent()
.expect("Failed to get parent directory")
.to_path_buf();

current_dir.to_string_lossy().to_string()
} else {
// Use app data dir in production
get_app()
.path()
.app_data_dir()
.expect("Failed to get app data dir")
.to_string_lossy()
.to_string()
};

fs::create_dir_all(&config_path).expect("Failed to create config directory");

// let config_file = Path::new(&config_dir).join("config.json");
let config_file_path = [&config_path, "config.json"]
let config_file_path = [&config_path, CONFIG_NAME]
.iter()
.collect::<PathBuf>()
.to_string_lossy()
.to_string();

let db_file_path = [&config_path, "clippy.sqlite"]
let db_file_path = [&config_path, DB_NAME]
.iter()
.collect::<PathBuf>()
.to_string_lossy()
Expand Down Expand Up @@ -122,11 +135,14 @@ pub async fn sync_clipboard_history_enable() {
let dir = dir.to_string();
let dir_file = format!("{}/clippy.sqlite", &dir);

println!("selected dir: {}", dir);

// check if backup file exists
if !Path::new(&dir_file).exists() {
// copy current database to backup location
printlog!(
"copying database to backup location {} {}",
&config.db,
&dir_file
);
fs::copy(&config.db, &dir_file).expect("Failed to copy database");
}

Expand Down Expand Up @@ -169,6 +185,7 @@ pub async fn sync_clipboard_history_disable() {
pub async fn sync_clipboard_history_toggle() {
let settings = get_settings().await.expect("Failed to get settings");

printlog!("synchronize: {}", settings.synchronize);
with_hotkeys(false, async move {
if settings.synchronize {
sync_clipboard_history_disable().await;
Expand Down
31 changes: 22 additions & 9 deletions src/components/elements/toggle.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
import { FiCheck } from "solid-icons/fi";
import { VsClose } from "solid-icons/vs";
import { Component, Setter } from "solid-js";
import { Component, Setter, createEffect, createSignal } from "solid-js";

type SwitchProps = {
checked?: boolean;
onChange: (val: boolean) => Promise<void> | Setter<boolean> | undefined;
};

export const Toggle: Component<SwitchProps> = (props) => {
let inputRef: HTMLInputElement | undefined;
const [internalChecked, setInternalChecked] = createSignal(props.checked || false);

createEffect(() => {
if (props.checked !== undefined) {
setInternalChecked(props.checked);
if (inputRef) {
inputRef.checked = props.checked;
}
}
});

const handleChange = () => {
const newValue = !internalChecked();
setInternalChecked(newValue);
props.onChange(newValue);
};

return (
<label class="relative inline-flex cursor-pointer items-center">
<input
type="checkbox"
checked={props.checked}
onChange={() => props.onChange(!props.checked)}
class="peer sr-only"
/>
<input ref={inputRef} type="checkbox" checked={internalChecked()} onChange={handleChange} class="peer sr-only" />
<div
class={`peer relative h-4 w-11 rounded-full bg-gray-200 after:absolute after:start-[2px] after:top-0 after:h-4 after:w-4 after:rounded-full after:border after:border-transparent after:bg-white after:transition-all after:content-[''] peer-checked:bg-indigo-600 peer-checked:after:translate-x-[150%] peer-checked:after:border-transparent peer-focus:outline-none dark:border-gray-600 rtl:peer-checked:after:-translate-x-full ${
props.checked ? "dark:bg-gray-700" : "dark:bg-red-600"
internalChecked() ? "dark:bg-gray-700" : "dark:bg-red-600"
} after:z-[40] dark:bg-opacity-20 after:dark:bg-zinc-700`}
>
<div class="absolute inset-0 z-[50] flex items-center justify-between px-1">
{props.checked ? <FiCheck class="ml-auto text-sm text-white" /> : <VsClose class="text-white" />}
{internalChecked() ? <FiCheck class="ml-auto text-sm text-white" /> : <VsClose class="text-white" />}
</div>
</div>
</label>
Expand Down
17 changes: 9 additions & 8 deletions src/components/navigation/app-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ import { HotkeyStore } from "../../store/hotkey-store";
interface AppSidebarProps {}

export const AppSidebar: Component<AppSidebarProps> = ({}) => {
const { hotkeys, globalHotkeyEvent, getHotkey } = HotkeyStore;
const { setCurrentTab, tabs } = AppStore;

return (
<Show when={hotkeys().length}>
<For each={tabs()}>
<Show when={HotkeyStore.hotkeys().length}>
<For each={AppStore.tabs()}>
{({ current, Icon, name, id }) => {
const currentHotkey = hotkeys()?.find((key) => key?.name === name);
const currentHotkey = HotkeyStore.hotkeys()?.find((key) => key?.name === name);

return (
<div
class={`${
current ? "text-black dark:text-white" : "text-zinc-600 dark:text-gray-dark"
} relative flex h-6 w-full cursor-pointer select-none items-center justify-center py-5 text-xl hover:text-black dark:hover:text-white`}
title={currentHotkey?.name}
onClick={() => setCurrentTab(id)}
onClick={() => AppStore.changeTab(id)}
>
<Icon title={name} />
<Show when={currentHotkey?.event && getHotkey(currentHotkey?.event) && globalHotkeyEvent()}>
<Show
when={
currentHotkey?.event && HotkeyStore.getHotkey(currentHotkey?.event) && HotkeyStore.globalHotkeyEvent()
}
>
<div class="absolute -top-0.5 left-1 rounded-sm bg-zinc-800 px-1 py-1 text-xs font-semibold text-white dark:bg-zinc-600">
{currentHotkey!.key}
</div>
Expand Down
Loading

0 comments on commit 4bde986

Please sign in to comment.