Skip to content

Commit

Permalink
[*] refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
heng30 committed Sep 7, 2024
1 parent 7f19db8 commit 881cdc2
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 37 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ repository = "https://github.com/Heng30/slint-template"
description = "It's a Rust Slint GUI framework project for quick start."

[dependencies]
log = "0.4"
slint = "1.7"
cfg-if = "1.0"

[target.'cfg(any(target_os = "windows", target_os = "linux", target_os = "macos", target_os = "android"))'.dependencies]
log = "0.4"
toml = "0.8"
anyhow = "1.0"
cfg-if = "1.0.0"
once_cell = "1.19"
webbrowser = "1.0"
derivative = "2.2"
Expand All @@ -41,6 +41,7 @@ android_logger = "0.13"
terminal-clipboard = "0.4"

[target.'cfg(target_arch = "wasm32")'.dependencies]
console_log = { version = "1.0", features = ["color"] }
wasm-bindgen = { version = "0.2" }
getrandom = { version = "0.2", features = ["js"] }

Expand Down
35 changes: 21 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ extern crate derivative;
))]
mod config;

#[cfg(any(
target_os = "windows",
target_os = "linux",
target_os = "macos",
target_os = "android"
))]
mod logic;

#[cfg(any(
target_os = "windows",
target_os = "linux",
Expand All @@ -51,6 +43,8 @@ mod version;
#[cfg(feature = "database")]
mod db;

mod logic;

#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
pub fn init_logger() {
use cutil::chrono::Local;
Expand Down Expand Up @@ -99,6 +93,12 @@ fn init_logger() {
);
}

#[cfg(target_arch = "wasm32")]
fn init_logger() {
use log::Level;
console_log::init_with_level(Level::Trace).expect("error initializing log");
}

#[cfg(any(
target_os = "windows",
target_os = "linux",
Expand All @@ -116,12 +116,11 @@ async fn ui_before() {
}
}

#[cfg(any(
target_os = "windows",
target_os = "linux",
target_os = "macos",
target_os = "android"
))]
#[cfg(target_arch = "wasm32")]
fn ui_before() {
init_logger();
}

fn ui_after(ui: &AppWindow) {
logic::init(ui);
}
Expand Down Expand Up @@ -159,9 +158,17 @@ pub async fn desktop_main() {
log::debug!("exit...");
}

#[cfg(target_arch = "wasm32")]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen::prelude::wasm_bindgen(start))]
pub fn main() {
log::debug!("start...");

ui_before();
let ui = AppWindow::new().unwrap();
ui.global::<Store>().set_device_type(DeviceType::Web);
ui_after(&ui);

ui.run().unwrap();

log::debug!("exit...");
}
8 changes: 8 additions & 0 deletions src/logic/examples.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
use crate::slint_generatedAppWindow::{AppWindow, Logic};
use slint::{ComponentHandle, Model, ModelRc, VecModel};

#[cfg(target_arch = "wasm32")]
pub fn init(ui: &AppWindow) {
ui.global::<Logic>().on_web_debug(move |text| {
log::debug!(text);
});
}

#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
pub fn init(ui: &AppWindow) {
ui.global::<Logic>()
.on_generate_search_values(move |entries| {
Expand Down
9 changes: 9 additions & 0 deletions src/logic/examples_web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::slint_generatedAppWindow::{AppWindow, Logic};

use slint::ComponentHandle;

pub fn init(ui: &AppWindow) {
ui.global::<Logic>().on_web_debug(move |text| {
log::debug!("{}", text);
});
}
63 changes: 56 additions & 7 deletions src/logic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,73 @@
use crate::slint_generatedAppWindow::AppWindow;

#[cfg(any(
target_os = "windows",
target_os = "linux",
target_os = "macos",
target_os = "android"
))]
mod about;

#[cfg(any(
target_os = "windows",
target_os = "linux",
target_os = "macos",
target_os = "android"
))]
mod clipboard;

#[cfg(any(
target_os = "windows",
target_os = "linux",
target_os = "macos",
target_os = "android"
))]
mod util;

#[cfg(any(
target_os = "windows",
target_os = "linux",
target_os = "macos",
target_os = "android"
))]
mod setting;

mod confirm_dialog;
mod popup_action;
mod setting;
mod toast;

#[allow(unused)]
mod tr;
mod util;

#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
mod examples;

#[cfg(target_arch = "wasm32")]
mod examples_web;

pub fn init(ui: &AppWindow) {
util::init(ui);
clipboard::init(ui);
#[cfg(any(
target_os = "windows",
target_os = "linux",
target_os = "macos",
target_os = "android",
))]
{
util::init(ui);
clipboard::init(ui);
about::init(ui);
setting::init(ui);
}

toast::init(ui);
confirm_dialog::init(ui);
popup_action::init(ui);
about::init(ui);
setting::init(ui);

examples::init(ui);
{
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
examples::init(ui);

#[cfg(target_arch = "wasm32")]
examples_web::init(ui);
}
}
20 changes: 18 additions & 2 deletions src/logic/tr.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
#[cfg(any(
target_os = "windows",
target_os = "linux",
target_os = "macos",
target_os = "android"
))]
use crate::config;

use std::collections::HashMap;

pub fn tr(text: &str) -> String {
if config::preference().language == "en" {
return text.to_string();
cfg_if::cfg_if! {
if #[cfg(any(
target_os = "windows",
target_os = "linux",
target_os = "macos",
target_os = "android"
))] {
if config::preference().language == "en" {
return text.to_string();
}
}
}

let items: HashMap<&str, &str> = HashMap::from([
Expand Down
2 changes: 1 addition & 1 deletion src/version.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub static VERSION: &str = "v0.1.0";
pub static VERSION: &str = "v0.1.1";
2 changes: 1 addition & 1 deletion ui/base/collapse.slint
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export component Collapse inherits Rectangle {
Image {
width: Theme.icon-size;
height: self.width;
colorize: Theme.icon-color;
colorize: entry.disabled ? Theme.disabled-color :Theme.icon-color;
source: entry.is-open ? root.close-icon : root.open-icon;
}
}
Expand Down
3 changes: 2 additions & 1 deletion ui/logic.slint
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export global Logic {
// (entries, search-value) -> entry
pure callback get-sidebar-key-from-search-values([SideBarEntry], string) -> string;

//////////////////////////////// Logic End ////////////////////////////////
callback web-debug(string);

//////////////////////////////// Logic End ////////////////////////////////
}
15 changes: 14 additions & 1 deletion ui/panel/web/home.slint
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Theme, Icons } from "../../theme.slint";
import { Logic } from "../../logic.slint";

import { CircleProgress } from "../../base/widgets.slint";
import { CircleProgress, ConfirmBtn } from "../../base/widgets.slint";
import { Util } from "../../util.slint";

component Body inherits Rectangle {
Expand All @@ -15,6 +15,19 @@ component Body inherits Rectangle {
progress: Util.progress-value(5s);
}
}

HorizontalLayout {
alignment: center;
ConfirmBtn {
use-auto-size: true;
text: "Click Me";

clicked => {
debug("debug from Slint");
Logic.web-debug("debug from Rust");
}
}
}
}
}

Expand Down
23 changes: 16 additions & 7 deletions ui/panel/web/panel.slint
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { Theme } from "../../theme.slint";
import { Store, TabIndex, SettingDetailIndex } from "../../store.slint";
import { Blanket, ToolTip, ToolTipSetting, PopupAction, PopupActionSetting } from "../../base/widgets.slint";

import { Home } from "./home.slint";

export component Bodyer inherits Rectangle {
if TabIndex.Home == Store.current-tab-index: Home { }
}
export component WebPanel inherits Rectangle {
Home { }

export component WebPanel inherits VerticalLayout {
Bodyer {
if false: Blanket {
clicked => {
}
}

if PopupActionSetting.is-show: PopupAction {
window-width: root.width;
window-height: root.height;
}

if ToolTipSetting.is-show: ToolTip {
window-width: root.width;
window-height: root.height;
}
}
1 change: 1 addition & 0 deletions ui/web-window.slint
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { WebPanel } from "panel/web/panel.slint";
import { LoadingStatus, Toast, ConfirmDialog, ConfirmDialogSetting, Blanket, ToastSetting, ToastStatus, PopupActionSetting } from "base/widgets.slint";

export component AppWindow inherits Window {
default-font-weight: Theme.normal-font-weight;
background: Theme.base-background;

WebPanel { }
Expand Down
2 changes: 1 addition & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="keyword" content="Programming, Rust, Slint" />
<meta property="og:site_name" content="Slint Template" />
<meta property="og:type" content="website" />
<link rel="dev icon" href="./ui/images/brand.png" />
<link rel="dev icon" href="../ui/images/brand.png" />
<link rel="shortcut icon" href="./pkg/favicon.png" />
<title>Slint Template</title>
</head>
Expand Down

0 comments on commit 881cdc2

Please sign in to comment.