qalc integrated
This commit is contained in:
parent
0f5818432c
commit
672d69ef75
11 changed files with 231 additions and 16 deletions
17
src/main.rs
17
src/main.rs
|
|
@ -5,7 +5,17 @@ use iced_layershell::reexport::Anchor;
|
|||
use iced_layershell::settings::{LayerShellSettings, Settings, StartMode};
|
||||
use iced_layershell::to_layer_message;
|
||||
|
||||
#[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() -> Result<(), iced_layershell::Error> {
|
||||
let _ = ffi::qalc_calculate("", 2000);
|
||||
|
||||
let binded_output_name = std::env::args().nth(1);
|
||||
let start_mode = match binded_output_name {
|
||||
Some(output) => StartMode::TargetScreen(output),
|
||||
|
|
@ -70,9 +80,10 @@ fn update(state: &mut State, message: Message) -> Command<Message> {
|
|||
Command::none()
|
||||
}
|
||||
Message::TextSubmit => {
|
||||
state
|
||||
.history
|
||||
.push(std::mem::take(&mut state.current_message));
|
||||
state.history.push(ffi::qalc_calculate(
|
||||
&std::mem::take(&mut state.current_message),
|
||||
2000,
|
||||
));
|
||||
|
||||
Command::none()
|
||||
}
|
||||
|
|
|
|||
20
src/qalc_bridge.cc
Normal file
20
src/qalc_bridge.cc
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#include "floating-calculator/src/qalc_bridge.h"
|
||||
#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();
|
||||
}
|
||||
return calc;
|
||||
}
|
||||
|
||||
rust::String qalc_calculate(rust::Str expression, int32_t timeout_ms) {
|
||||
Calculator *calc = get_calculator();
|
||||
std::string expr(expression.data(), expression.size());
|
||||
std::string result = calc->calculateAndPrint(expr, timeout_ms);
|
||||
return rust::String(result);
|
||||
}
|
||||
5
src/qalc_bridge.h
Normal file
5
src/qalc_bridge.h
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#pragma once
|
||||
#include "rust/cxx.h"
|
||||
#include <string>
|
||||
|
||||
rust::String qalc_calculate(rust::Str expression, int32_t timeout_ms);
|
||||
Loading…
Add table
Add a link
Reference in a new issue