From 1a0020cf1b1e3ffe024fafc85322b8959af0d7a1 Mon Sep 17 00:00:00 2001 From: kate Date: Tue, 18 Feb 2025 12:38:20 +0800 Subject: [PATCH 1/4] Ignore fltk-related things when compiling without the 'gui' feature --- Cargo.toml | 14 +++++--- build.rs | 7 ++-- src/gui/dialog.rs | 19 +++++++++++ src/gui/dialog_headless.rs | 66 ++++++++++++++++++++++++++++++++++++++ src/gui/mod.rs | 62 ++++++++++++----------------------- 5 files changed, 119 insertions(+), 49 deletions(-) create mode 100644 src/gui/dialog_headless.rs diff --git a/Cargo.toml b/Cargo.toml index be43ea5..25f26e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,10 @@ description = "Installer for Open Fortress" repository = "https://github.com/ktwrd/beans-rs" license = "GPL-3.0" +[features] +default = [] +gui = ["fl2rust", "fltk", "fltk-theme", "dark-light", "image"] + [dependencies] async-recursion = "1.1.1" async-stream = "0.3.5" @@ -35,13 +39,13 @@ colored = "3.0.0" sentry-log = "0.36.0" chrono = "0.4.38" -fltk = { version = "1.5.1" } -fltk-theme = "0.7.4" -dark-light = "2.0.0" -image = { version = "0.25.5", features = ["png"] } +fltk = { version = "1.5.1", optional = true } +fltk-theme = {version = "0.7.4",optional = true } +dark-light = {version = "2.0.0", optional = true } +image = { version = "0.25.5", features = ["png"], optional = true } [build-dependencies] -fl2rust = "0.6.0" +fl2rust = { version = "0.6.0", optional = true } [target.'cfg(target_os = "windows")'.dependencies] winconsole = { version = "0.11.1", features = ["window"] } diff --git a/build.rs b/build.rs index b9f4873..b568325 100644 --- a/build.rs +++ b/build.rs @@ -1,4 +1,3 @@ -use std::path::PathBuf; #[allow(dead_code, unused_macros, unused_imports)] use std::{env, io}; @@ -21,11 +20,12 @@ fn main() } /// generate files for fltk ui stuff +#[cfg(feature = "gui")] fn fltk() -> Result<(), BuildError> { println!("cargo:rerun-if-changed=src/gui/shared_ui.fl"); let g = fl2rust::Generator::default(); - let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + let out_path = std::path::PathBuf::from(env::var("OUT_DIR").unwrap()); if let Err(e) = g.in_out( "src/gui/shared_ui.fl", out_path.join("shared_ui.rs").to_str().unwrap() @@ -39,6 +39,9 @@ fn fltk() -> Result<(), BuildError> Ok(()) } +#[cfg(not(feature = "gui"))] +fn fltk() -> Result<(), BuildError> +{Ok(())} /// check if a location exists #[allow(dead_code)] diff --git a/src/gui/dialog.rs b/src/gui/dialog.rs index cb1cb72..c1b0473 100644 --- a/src/gui/dialog.rs +++ b/src/gui/dialog.rs @@ -2,6 +2,7 @@ use fltk::{image::PngImage, prelude::*, text::TextBuffer, *}; +use fltk::window::Window; use log::warn; use crate::gui::{apply_app_scheme, @@ -113,3 +114,21 @@ impl DialogBuilder wait_for_quit(&app, &receive_action); } } + + +/// Make the `window` provided the in be the center of the current screen. +fn window_centre_screen(window: &mut Window) +{ + let (sx, sy) = app::screen_coords(); + let width = window.width(); + let height = window.height(); + let (mut x, mut y) = app::screen_size(); + x -= width as f64; + y -= height as f64; + window.resize( + ((x / 2.0) as i32) + sx, + ((y / 2.0) as i32) + sy, + width, + height + ); +} \ No newline at end of file diff --git a/src/gui/dialog_headless.rs b/src/gui/dialog_headless.rs new file mode 100644 index 0000000..0078e77 --- /dev/null +++ b/src/gui/dialog_headless.rs @@ -0,0 +1,66 @@ +pub struct DialogBuilder +{ + pub title: String, + pub content: String, +} + +pub enum DialogIconKind +{ + Default, + Warn, + Error +} + +impl Default for crate::gui::DialogBuilder +{ + fn default() -> Self + { + Self { + title: format!("beans v{}", crate::VERSION), + content: String::new() + } + } +} + +impl crate::gui::DialogBuilder +{ + pub fn new() -> Self + { + Self::default() + } + pub fn with_png_data( + mut self, + data: &[u8] + ) -> Self + { + self + } + pub fn with_icon( + self, + kind: crate::gui::DialogIconKind + ) -> Self + { + self + } + pub fn with_title( + mut self, + content: String + ) -> Self + { + self.title = content.clone(); + self + } + pub fn with_content( + mut self, + content: String + ) -> Self + { + self.content = content.clone(); + self + } + pub fn run(&self) + { + println!("============ {} ============", self.title); + println!("{}", self.content); + } +} \ No newline at end of file diff --git a/src/gui/mod.rs b/src/gui/mod.rs index b64d8b8..b48a27e 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -1,15 +1,25 @@ +#[cfg(feature = "gui")] use fltk::{app::Receiver, prelude::*, window::Window, *}; +#[cfg(feature = "gui")] use fltk_theme::{color_themes, ColorTheme}; +#[cfg(feature = "gui")] use log::debug; +#[cfg(feature = "gui")] mod dialog; +#[cfg(not(feature = "gui"))] +mod dialog_headless; +#[cfg(feature = "gui")] pub(crate) mod shared_ui; +#[cfg(feature = "gui")] pub use dialog::*; +#[cfg(not(feature = "gui"))] +pub use dialog_headless::*; pub mod icon; @@ -33,24 +43,12 @@ pub enum GUIAppStatus BtnContinue } -/// Make the `window` provided the in be the center of the current screen. -pub fn window_centre_screen(window: &mut Window) -{ - let (sx, sy) = app::screen_coords(); - let width = window.width(); - let height = window.height(); - let (mut x, mut y) = app::screen_size(); - x -= width as f64; - y -= height as f64; - window.resize( - ((x / 2.0) as i32) + sx, - ((y / 2.0) as i32) + sy, - width, - height - ); -} +#[cfg(not(feature = "gui"))] +pub fn get_center_screen() -> (i32, i32) +{(0, 0)} /// Get the X and Y position of the center of the current screen. +#[cfg(feature = "gui")] pub fn get_center_screen() -> (i32, i32) { let (px, py) = app::screen_coords(); @@ -58,31 +56,10 @@ pub fn get_center_screen() -> (i32, i32) (((sw / 2.0) as i32) + px, ((sh / 2.0) as i32) + py) } -/// Ensure that a window has a fixed width & height, and that it will appear in -/// the centre of the current screen. -pub fn window_ensure( - win: &mut Window, - width: i32, - height: i32 -) -{ - window_centre_screen(win); - win.handle(move |w, ev| match ev - { - fltk::enums::Event::Resize => - { - if w.width() > width || w.height() > height - { - w.set_size(width, height); - } - true - } - _ => false - }); - win.make_resizable(false); - win.show(); -} - +#[cfg(not(feature = "gui"))] +pub fn apply_app_scheme() +{} +#[cfg(feature = "gui")] pub fn apply_app_scheme() { let theme_content = match dark_light::detect() @@ -109,7 +86,8 @@ pub fn apply_app_scheme() theme.apply(); } -pub fn wait_for_quit( +#[cfg(feature = "gui")] +pub(crate) fn wait_for_quit( app: &app::App, receive_action: &Receiver ) From c855ca4b89ab7d7ced730011f382464a01c5bcdb Mon Sep 17 00:00:00 2001 From: kate Date: Tue, 18 Feb 2025 12:38:38 +0800 Subject: [PATCH 2/4] Updated workflows to support gui and non-gui builds --- .github/workflows/compile.yml | 12 ++++++++++-- .github/workflows/release.yml | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/.github/workflows/compile.yml b/.github/workflows/compile.yml index 3dde057..7d96ce9 100644 --- a/.github/workflows/compile.yml +++ b/.github/workflows/compile.yml @@ -18,9 +18,17 @@ jobs: - os: ubuntu-20.04 filename: 'beans-rs' target: x86_64-unknown-linux-gnu + feature: gui + + - os: ubuntu-20.04 + filename: 'beans-rs' + target: x86_64-unknown-linux-musl + feature: default + - os: windows-latest target: x86_64-pc-windows-msvc filename: 'beans-rs.exe' + feature: gui runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 @@ -60,7 +68,7 @@ jobs: - uses: actions-rs/cargo@v1 with: command: build - args: --verbose --target ${{ matrix.target }} + args: --verbose --target ${{ matrix.target }} -F ${{ matrix.feature }} - name: Code Signing (Windows) if: ${{ matrix.os == 'windows-latest' }} uses: skymatic/code-sign-action@v1 @@ -73,5 +81,5 @@ jobs: - name: Upload artifact uses: actions/upload-artifact@v4 with: - name: binary-${{ matrix.os }}-${{ matrix.target }} + name: binary-${{ matrix.os }}-${{ matrix.target }}-${{ matrix.feature }} path: target/${{ matrix.target }}/debug/${{ matrix.filename }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0091f4d..45d7433 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -17,11 +17,20 @@ jobs: include: - os: ubuntu-20.04 filename: 'beans-rs' + output_filename: 'beans-rs' target: x86_64-unknown-linux-gnu + feature: gui + - os: ubuntu-20.04 + filename: 'beans-rs' + output_filename: 'beans-rs-nogui' + target: x86_64-unknown-linux-musl + feature: default - os: windows-latest target: x86_64-pc-windows-msvc filename: 'beans-rs.exe' + output_filename: 'beans-rs.exe' + feature: gui steps: - uses: actions/checkout@master @@ -61,7 +70,7 @@ jobs: - uses: actions-rs/cargo@v1 with: command: build - args: --release --target ${{ matrix.target }} + args: --release --target ${{ matrix.target }} -F ${{ matrix.feature }} - name: Code Signing (Windows) if: ${{ matrix.os == 'windows-latest' }} @@ -73,10 +82,15 @@ jobs: description: 'beans-rs' certificatesha1: '${{ secrets.CODESIGN_HASH }}' + - name: Rename output file + if: ${{ matrix.filename != matrix.output_filename }} + shell: pwsh + run: Move-Item -Path target/${{ matrix.target }}/release/${{ matrix.filename }} -Destination target/${{ matrix.target }}/release/${{ matrix.output_filename }} + - name: Upload binaries to release uses: softprops/action-gh-release@v1 with: - files: target/${{ matrix.target }}/release/${{ matrix.filename }} + files: target/${{ matrix.target }}/release/${{ matrix.output_filename }} tag_name: ${{ github.event.inputs.tag }} draft: false prerelease: true From 124bd518f835cdb98932e227c793769e8ed36aa5 Mon Sep 17 00:00:00 2001 From: kate Date: Tue, 18 Feb 2025 12:43:49 +0800 Subject: [PATCH 3/4] Updated dialog.rs --- src/gui/dialog.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/gui/dialog.rs b/src/gui/dialog.rs index c1b0473..53253c6 100644 --- a/src/gui/dialog.rs +++ b/src/gui/dialog.rs @@ -9,7 +9,6 @@ use crate::gui::{apply_app_scheme, icon, shared_ui::GenericDialog, wait_for_quit, - window_centre_screen, GUIAppStatus}; pub struct DialogBuilder From 4c1907fc048b028858beccde1fafb85fd8c5b15e Mon Sep 17 00:00:00 2001 From: kate Date: Tue, 18 Feb 2025 12:49:01 +0800 Subject: [PATCH 4/4] Updated workflows to install musl-tools --- .github/workflows/compile.yml | 3 ++- .github/workflows/release.yml | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/compile.yml b/.github/workflows/compile.yml index 7d96ce9..c41f9cb 100644 --- a/.github/workflows/compile.yml +++ b/.github/workflows/compile.yml @@ -58,7 +58,8 @@ jobs: libxinerama-dev \ libfltk1.3 \ libfltk1.3-dev \ - libssl-dev + libssl-dev \ + musl-tools - uses: actions-rs/toolchain@v1 with: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 45d7433..6ef45c0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -60,7 +60,8 @@ jobs: libxinerama-dev \ libfltk1.3 \ libfltk1.3-dev \ - libssl-dev + libssl-dev \ + musl-tools - uses: actions-rs/toolchain@v1 with: