From 8f91475499358fc9019bf5dacd40e3b24d9636d6 Mon Sep 17 00:00:00 2001 From: maxstrb Date: Thu, 26 Feb 2026 10:27:06 +0100 Subject: [PATCH] Ready to make it pretty --- Cargo.toml | 3 ++ reee.rs | 127 --------------------------------------------- src/main.rs | 31 ++++++++--- src/qalc_bridge.cc | 20 +++---- 4 files changed, 39 insertions(+), 142 deletions(-) delete mode 100644 reee.rs diff --git a/Cargo.toml b/Cargo.toml index 18190a2..6b90dfa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,6 @@ codegen-units = 1 strip = "symbols" panic = "abort" debug = false + +[profile.dev] +opt-level = 1 diff --git a/reee.rs b/reee.rs deleted file mode 100644 index 3c468ee..0000000 --- a/reee.rs +++ /dev/null @@ -1,127 +0,0 @@ -use iced::theme::Style; -use iced::widget::{Column, Container, Id, TextInput, container, operation, text}; -use iced::{Background, Color, Element, Event, Task as Command, window}; - -#[cxx::bridge] -mod ffi { - unsafe extern "C++" { - include!("floating-calculator/src/qalc_bridge.h"); - fn qalc_calculate(expression: &str, timeout_ms: i32) -> String; - } -} - -pub fn main() -> iced::Result { - let _ = ffi::qalc_calculate("", 2000); - - iced::application(State::default, State::update, State::view) - .title("Iced qalc calculator") - .subscription(State::subscription) - .style(style) - .window(window::Settings { - size: iced::Size::new(400.0, 400.0), - transparent: true, - decorations: false, - level: window::Level::AlwaysOnTop, - ..Default::default() - }) - .run() -} - -struct State { - initialized: bool, - current_message: String, - history: Vec, - input_id: Id, -} - -impl Default for State { - fn default() -> Self { - Self { - initialized: false, - current_message: String::new(), - history: Vec::new(), - input_id: Id::unique(), - } - } -} - -#[derive(Debug, Clone)] -enum Message { - Init, - TextSubmit, - TextInput(String), - IcedEvent(Event), -} - -impl State { - fn subscription(&self) -> iced::Subscription { - use iced::event; - use iced::futures::stream; - - let init = if !self.initialized { - iced::Subscription::run_with("init", |_| stream::once(async { Message::Init })) - } else { - iced::Subscription::none() - }; - - iced::Subscription::batch([init, event::listen().map(Message::IcedEvent)]) - } - - fn update(&mut self, message: Message) -> Command { - use iced::keyboard::{Key, key::Named}; - - match message { - Message::Init => { - self.initialized = true; - operation::focus(self.input_id.clone()) - } - Message::IcedEvent(event) => { - if let Event::Keyboard(iced::keyboard::Event::KeyReleased { - key: Key::Named(Named::Escape), - .. - }) = event - { - return iced::exit(); - } - Command::none() - } - Message::TextInput(t) => { - self.current_message = t; - Command::none() - } - Message::TextSubmit => { - self.history.push(ffi::qalc_calculate( - &std::mem::take(&mut self.current_message), - 2000, - )); - Command::none() - } - } - } - - fn view(&self) -> Container<'_, Message> { - let input: TextInput<'_, Message> = - TextInput::new("Type something here...", &self.current_message) - .on_input(Message::TextInput) - .on_submit(Message::TextSubmit) - .id(self.input_id.clone()); - - let content = Column::with_children( - std::iter::once(input.into()) - .chain(self.history.iter().rev().map(|msg| text!("{msg}").into())) - .collect::>>(), - ); - - container(content).style(|_theme| container::Style { - background: Some(Background::Color(Color::TRANSPARENT)), - ..Default::default() - }) - } -} - -fn style(_state: &State, theme: &iced::Theme) -> Style { - Style { - background_color: Color::TRANSPARENT, - text_color: theme.palette().text, - } -} diff --git a/src/main.rs b/src/main.rs index d126c04..dee3c82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,8 @@ use iced_layershell::reexport::Anchor; use iced_layershell::settings::{LayerShellSettings, Settings, StartMode}; use iced_layershell::to_layer_message; +use crate::ffi::qalc_calculate; + #[cxx::bridge] mod ffi { unsafe extern "C++" { @@ -41,10 +43,21 @@ pub fn main() -> Result<(), iced_layershell::Error> { struct State { initialized: bool, current_message: String, - history: Vec, + history: Vec, input_id: Id, } +struct History { + promt: String, + output: String, +} + +impl History { + fn new(promt: String, output: String) -> Self { + Self { promt, output } + } +} + impl Default for State { fn default() -> Self { Self { @@ -106,10 +119,10 @@ fn update(state: &mut State, message: Message) -> Command { Command::none() } Message::TextSubmit => { - state.history.push(ffi::qalc_calculate( - &std::mem::take(&mut state.current_message), - 2000, - )); + let result = qalc_calculate(&state.current_message, 2000); + let history = History::new(std::mem::take(&mut state.current_message), result); + + state.history.push(history); Command::none() } @@ -127,7 +140,13 @@ fn view(state: &State) -> Container<'_, Message> { let content = Column::with_children( std::iter::once(input.into()) - .chain(state.history.iter().rev().map(|msg| text!("{msg}").into())) + .chain( + state + .history + .iter() + .rev() + .map(|msg| text!("{}", msg.output).into()), + ) .collect::>>(), ); diff --git a/src/qalc_bridge.cc b/src/qalc_bridge.cc index 3aad822..4730c3d 100644 --- a/src/qalc_bridge.cc +++ b/src/qalc_bridge.cc @@ -2,19 +2,21 @@ #include "floating-calculator/src/main.rs.h" #include -static Calculator *get_calculator() { - static Calculator *calc = nullptr; - if (!calc) { - calc = new Calculator(); - calc->loadGlobalDefinitions(); - calc->loadLocalDefinitions(); - } +Calculator create_calculator() { + Calculator calc; + calc.loadGlobalDefinitions(); + calc.loadLocalDefinitions(); + return calc; +} + +Calculator &get_calculator() { + static Calculator calc = create_calculator(); return calc; } rust::String qalc_calculate(rust::Str expression, int32_t timeout_ms) { - Calculator *calc = get_calculator(); + Calculator &calc = get_calculator(); std::string expr(expression.data(), expression.size()); - std::string result = calc->calculateAndPrint(expr, timeout_ms); + std::string result = calc.calculateAndPrint(expr, timeout_ms); return rust::String(result); }