uuuuuuhhhhhhh

This commit is contained in:
maxstrb 2026-03-01 21:45:41 +01:00
parent 670f7a5c70
commit 89e8300bfb
12 changed files with 33 additions and 310 deletions

View file

@ -1,81 +1,14 @@
mod state;
mod update;
mod view;
mod qalc;
use crate::qalc::ffi::qalc_calculate;
use crate::state::Message;
use crate::update::update;
use crate::view::view;
use iced::widget::{container, text_input};
use iced::{Background, Border, Color};
use iced_layershell::application;
use iced_layershell::reexport::Anchor;
use iced_layershell::settings::{LayerShellSettings, Settings};
pub fn main() -> iced::Result {}
include!(concat!(env!("OUT_DIR"), "/theme.rs"));
pub fn main() -> Result<(), iced_layershell::Error> {
application(
State::default,
|| "floating-calculator".into(),
update,
view,
)
.theme(theme)
.style(style)
.subscription(subscription)
.settings(Settings {
layer_settings: LayerShellSettings {
size: Some(WINDOW_SIZE),
exclusive_zone: -1,
anchor: Anchor::empty(),
..Default::default()
},
..Default::default()
})
.run()
struct Calculator {
current_input: String,
history: Vec<History>,
}
fn subscription(_state: &State) -> iced::Subscription<Message> {
use iced::event;
use iced::futures::stream;
iced::Subscription::batch([
iced::Subscription::run_with("init", |_| stream::once(async { Message::Init })),
event::listen().map(Message::IcedEvent),
])
}
fn input_style(theme: &iced::Theme, _status: text_input::Status) -> text_input::Style {
let ext = theme.extended_palette();
text_input::Style {
background: Background::Color(Color::TRANSPARENT),
border: Border::default(),
icon: Color::TRANSPARENT,
placeholder: ext.background.weak.text,
value: ext.background.base.text,
selection: ext.primary.weak.color,
}
}
fn container_style(theme: &iced::Theme) -> container::Style {
let ext = theme.extended_palette();
let mut bg = ext.background.base.color;
bg.a = BG_ALPHA;
container::Style {
text_color: Some(ext.background.base.text),
background: Some(Background::Color(bg)),
border: Border::default()
.rounded(BORDER_RADIUS)
.color(ext.primary.base.color)
.width(BORDER_WIDTH),
..Default::default()
}
}
fn style(_state: &State, theme: &iced::Theme) -> iced::theme::Style {
iced::theme::Style {
background_color: Color::TRANSPARENT,
text_color: theme.palette().text,
}
struct History {
prompt: String,
result: String,
}

7
src/qalc/mod.rs Normal file
View file

@ -0,0 +1,7 @@
#[cxx::bridge]
pub mod ffi {
unsafe extern "C++" {
include!("floating-calculator/src/qalc/qalc_bridge.h");
pub fn qalc_calculate(expression: &str, timeout_ms: i32) -> String;
}
}

View file

@ -1,5 +1,5 @@
#include "floating-calculator/src/qalc_bridge.h"
#include "floating-calculator/src/update.rs.h"
#include "floating-calculator/src/qalc/qalc_bridge.h"
#include "floating-calculator/src/qalc/mod.rs.h"
#include <libqalculate/qalculate.h>
Calculator create_calculator() {

View file

@ -1,39 +0,0 @@
use iced::Event;
use iced::widget::Id;
use iced_layershell::to_layer_message;
pub struct State {
pub current_message: String,
pub history: Vec<History>,
pub input_id: Id,
}
pub struct History {
pub promt: String,
pub output: String,
}
impl History {
pub fn new(promt: String, output: String) -> Self {
Self { promt, output }
}
}
impl Default for State {
fn default() -> Self {
Self {
current_message: String::new(),
history: Vec::new(),
input_id: Id::unique(),
}
}
}
#[to_layer_message]
#[derive(Debug, Clone)]
pub enum Message {
Init,
TextSubmit,
TextInput(String),
IcedEvent(Event),
}

View file

@ -1,45 +0,0 @@
use crate::state::{History, Message, State};
use iced::keyboard::Event::KeyReleased;
use iced::keyboard::key::{Key, Named};
use iced::widget::operation;
use iced::{Event, Task as Command};
#[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 update(state: &mut State, message: Message) -> Command<Message> {
match message {
Message::Init => operation::focus(state.input_id.clone()),
Message::IcedEvent(event) => {
if let Event::Keyboard(KeyReleased {
key: Key::Named(Named::Escape),
..
}) = event
{
return iced::exit();
}
Command::none()
}
Message::TextInput(text) => {
state.current_message = text;
Command::none()
}
Message::TextSubmit => {
let result = ffi::qalc_calculate(&state.current_message, 2000);
let history = History::new(std::mem::take(&mut state.current_message), result);
state.history.push(history);
Command::none()
}
_ => Command::none(),
}
}

View file

@ -1,47 +0,0 @@
use crate::state::{Message, State};
use iced::widget::{Column, TextInput, column, container, text, text_input};
use iced::{Background, Border, Color, Element, color};
pub fn view(state: &State) -> Element<'_, Message> {
let input: TextInput<'_, Message> =
TextInput::new("Type something here...", &state.current_message)
.on_input(Message::TextInput)
.on_submit(Message::TextSubmit)
.id(state.input_id.clone());
let input = input.style(|_theme, _status| text_input::Style {
background: Background::Color(Color::TRANSPARENT),
border: Border::default(),
icon: Color::TRANSPARENT,
placeholder: color!(0xbbbbbb),
value: Color::WHITE,
selection: color!(0x0000ff),
});
let content = Column::with_children(
std::iter::once(input.into())
.chain(state.history.iter().rev().map(|msg| {
column![
text!("{}", msg.promt).size(12).color(color!(0x999999)),
text!(" {}", msg.output)
]
.into()
}))
.collect::<Vec<Element<_>>>(),
);
container(content)
.width(iced::Length::Fill)
.height(iced::Length::Fill)
.padding(8.)
.style(|_theme| container::Style {
text_color: Some(Color::WHITE),
background: Some(iced::Background::Color(Color::from_rgba8(30, 30, 46, 0.75))),
border: iced::Border::default()
.rounded(15.)
.color(color!(0xaaaaff))
.width(2.),
..Default::default()
})
.into()
}