Ready to make it pretty
This commit is contained in:
parent
1402bea078
commit
8f91475499
4 changed files with 39 additions and 142 deletions
|
|
@ -21,3 +21,6 @@ codegen-units = 1
|
|||
strip = "symbols"
|
||||
panic = "abort"
|
||||
debug = false
|
||||
|
||||
[profile.dev]
|
||||
opt-level = 1
|
||||
|
|
|
|||
127
reee.rs
127
reee.rs
|
|
@ -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<String>,
|
||||
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<Message> {
|
||||
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<Message> {
|
||||
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::<Vec<Element<_>>>(),
|
||||
);
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
31
src/main.rs
31
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<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<_>>>(),
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue