Ready to make it pretty

This commit is contained in:
maxstrb 2026-02-26 10:27:06 +01:00
parent 1402bea078
commit 8f91475499
4 changed files with 39 additions and 142 deletions

View file

@ -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<String>,
history: Vec<History>,
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<Message> {
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::<Vec<Element<_>>>(),
);

View file

@ -2,19 +2,21 @@
#include "floating-calculator/src/main.rs.h"
#include <libqalculate/qalculate.h>
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);
}