Skip to content

Commit

Permalink
Released trigonometry module and it's env. Many features edited.
Browse files Browse the repository at this point in the history
modified:   .gitignore
modified:   Cargo.toml
modified:   src/gui/app.rs
modified:   src/gui/defaults.rs
modified:   src/gui/scenes.rs
modified:   src/gui/tools.rs
new file:   src/helpers.rs
modified:   src/instruments/mod.rs
modified:   src/instruments/qe.rs
modified:   src/instruments/trigonometry.rs
modified:   src/lib.rs
modified:   src/main.rs
modified:   src/settings.rs
  • Loading branch information
DarkJoij committed Sep 17, 2023
1 parent 00e2d99 commit 5d59d46
Show file tree
Hide file tree
Showing 13 changed files with 388 additions and 118 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Folders:
/.idea
/target

# Files:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "deep-math-helper"
version = "0.23.914-alpha"
version = "0.23.917-alpha"
edition = "2021"
authors = ["DarkJoij"]
description = "An application for performing mathematical calculations*."
Expand Down
27 changes: 19 additions & 8 deletions src/gui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{displayable_err, if_ultimate_version};
use crate::gui::scenes::get_scene;
use crate::gui::tools::{Message, Page, ShortElement};
use crate::instruments::{Container, DataStore, DisplayableResult};
use crate::helpers::PseudoIterator;
use crate::settings::write_file;

use iced::executor::Default;
Expand Down Expand Up @@ -33,6 +34,8 @@ impl Application for DeepMathHelper {
}

fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
let none = Command::none();

match message {
Message::SwitchTheme => {
self.data.settings.theme.switch();
Expand All @@ -44,12 +47,12 @@ impl Application for DeepMathHelper {
eprintln!("{} {error}", &message);
}

self.data.pending.push(displayable_err!(message));
self.data.container.pending.push(displayable_err!(message));
}
},
Message::SetPage(page) => {
if let Page::Selection = page {
self.data.pending.clear();
self.data.container.pending.clear();
self.data.container = Container::default();
}

Expand All @@ -76,19 +79,27 @@ impl Application for DeepMathHelper {
Message::UpdateCell4(cell_4) => {
self.data.container.cell_4 = cell_4;
},
Message::SwitchTrigonometricPart => {
self.data.container.part = self.data.container.part.next();
},
Message::SwitchTrigonometricUnit => {
self.data.container.unit = self.data.container.unit.next();
},
Message::Calculate => {
let result = self.data.container.calculate(&self.data);
let result = self.data.container.calculate(&self.data.current_page);

// This is weird mechanism.
if let DisplayableResult::Success(..) = result {
self.data.pending.clear();
if result.is_success() || self.data.container.pending.len() >= 10 {
self.data.container.pending.clear();
}
else if let DisplayableResult::None = result {
return none;
}

self.data.pending.push(result);
self.data.container.pending.push(result);
}
};

Command::none()
none
}

fn view(&self) -> ShortElement<'_> {
Expand Down
20 changes: 10 additions & 10 deletions src/gui/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ pub fn get_default_row<'a>() -> Row<'a, Message> {
pub fn get_default_column<'a>() -> Column<'a, Message> {
Column::new()
.align_items(Alignment::Center)
.padding(Padding::new(35.))
.padding(Padding::new(35f32))
}

pub fn get_default_button<'a>(content: &'a str, message: Message) -> Button<'a, Message> {
pub fn get_default_button(content: &str, message: Message) -> Button<Message> {
Button::new(content)
.on_press(message)
.width(Length::Fill)
}

pub fn get_default_text<'a>(content: String) -> Text<'a> {
Text::new(content)
.horizontal_alignment(Horizontal::Center)
.vertical_alignment(Vertical::Center)
.width(Length::Fill)
}

pub fn get_default_text_input<'a, M>(placeholder: &str, value: &str, message: M) -> TextInput<'a, Message>
where
M: 'a + Fn(String) -> Message
{
TextInput::new(placeholder, value)
.padding(5)
.padding(5u16)
.on_input(message)
.width(Length::Fill)
}

pub fn get_default_text<'a>(content: String) -> Text<'a> {
Text::new(content)
.horizontal_alignment(Horizontal::Center)
.vertical_alignment(Vertical::Center)
.width(Length::Fill)
}
75 changes: 50 additions & 25 deletions src/gui/scenes.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::gui::defaults::*;
use crate::gui::tools::{Message, Page, ShortElement};
use crate::instruments::{DataStore, DisplayableResult};
use crate::settings::ThemeStrings;
use crate::helpers::{AsStr, Part, ThemeStrings};

use iced::Alignment;
use iced::widget::{Button, Column};

pub fn get_scene<'a>(data: &'a DataStore) -> ShortElement<'a> {
pub fn get_scene(data: &DataStore) -> ShortElement {
let mut main = get_default_column();
let mut current_page = match data.current_page {
Page::Selection => get_selection_page(),
Expand All @@ -25,11 +25,11 @@ pub fn get_scene<'a>(data: &'a DataStore) -> ShortElement<'a> {
// ... to here code must be refactored.
main = main.push(current_page);

if data.pending.len() == 0 {
if data.container.pending.is_empty() {
return main.into()
}

for part in &data.pending {
for part in &data.container.pending {
let content = match part {
DisplayableResult::None => continue,
DisplayableResult::Error(message) => format!("Ошибка: {message}"),
Expand Down Expand Up @@ -76,26 +76,6 @@ fn get_quadratic_equations_page<'a>(data: &DataStore) -> Column<'a, Message> {
.push(coefficients)
}

fn get_trigonometry_pages<'a>(data: &DataStore) -> Column<'a, Message> {
let field_1 = &data.container.cell_1;
let field_2 = &data.container.cell_2;
let field_3 = &data.container.cell_3;
let field_4 = &data.container.cell_4;

let text = get_default_text("Тригонометрические функции:".to_owned());

let functions = Column::new() // Must be without padding.
.align_items(Alignment::Center)
.push(get_default_text_input("sin", field_1, Message::UpdateCell1))
.push(get_default_text_input("cos", field_2, Message::UpdateCell3))
.push(get_default_text_input("tan", field_3, Message::UpdateCell3))
.push(get_default_text_input("cot", field_4, Message::UpdateCell4));

get_default_column()
.push(text)
.push(functions)
}

fn get_bases_converter_page<'a>(data: &DataStore) -> Column<'a, Message> {
let number_literal = &data.container.cell_1;
let from_base = &data.container.cell_2;
Expand All @@ -110,7 +90,52 @@ fn get_bases_converter_page<'a>(data: &DataStore) -> Column<'a, Message> {
.push(number_and_bases)
}

fn get_theme_button<'a>(data: &'a DataStore) -> Button<'a, Message> {
fn get_trigonometry_pages(data: &DataStore) -> Column<Message> {
let part = data.container.part.as_str();
let unit = data.container.unit.as_str();
let literals = [
"sin", "cos", "tan", "cot"
];
let fields = [
&data.container.cell_1, &data.container.cell_2,
&data.container.cell_3, &data.container.cell_4
];
let messages = [
Message::UpdateCell1, Message::UpdateCell2,
Message::UpdateCell3, Message::UpdateCell4
];

let title = get_default_text("Тригонометрические функции:".to_owned());

let switchers = get_default_row()
.push(Button::new(part)
.on_press(Message::SwitchTrigonometricPart))
.push(Button::new(unit)
.on_press(Message::SwitchTrigonometricUnit));

let mut functions = Column::new()
.align_items(Alignment::Center);

for index in 0..4 {
let mut literal = String::new();
let field = fields[index];
let message = messages[index];

if let Part::ArcFunctions = data.container.part {
literal.push('a');
}

literal.push_str(literals[index]);
functions = functions.push(get_default_text_input(&literal, field, message));
}

get_default_column()
.push(switchers)
.push(title)
.push(functions)
}

fn get_theme_button(data: &DataStore) -> Button<Message> {
Button::new(data.settings.theme.display_name())
.on_press(Message::SwitchTheme)
}
Expand Down
2 changes: 2 additions & 0 deletions src/gui/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub enum Message {
UpdateCell2(String),
UpdateCell3(String),
UpdateCell4(String),
SwitchTrigonometricPart,
SwitchTrigonometricUnit,
Calculate
}

Expand Down
143 changes: 143 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
use std::fmt::{Debug, Display, Formatter, Result};

use iced::Theme;

// [`Clone`] needed for [`Switcher`].
#[derive(Copy, Clone, Default, PartialEq)]
pub enum Part {
#[default]
Fundamental,
ArcFunctions
}

// [`Clone`] needed for [`Switcher`].
#[derive(Copy, Clone, Default, PartialEq)]
pub enum Unit {
#[default]
Radians,
Degrees
}

pub trait PseudoIterator: Sized {
fn all(&self) -> &[Self];
fn next(&self) -> Self;
}

impl PseudoIterator for Part {
fn all(&self) -> &[Self] {
&[Self::Fundamental, Self::ArcFunctions]
}

fn next(&self) -> Self {
let parts = self.all();
let parts_iter = parts.iter();

for (index, part) in parts_iter.enumerate() {
if self == part {
return match parts.get(index + 1) {
None => Self::Fundamental,
Some(part) => *part
};
}
}

Self::Fundamental
}
}

impl PseudoIterator for Unit {
fn all(&self) -> &[Self] {
&[Self::Radians, Self::Degrees]
}

fn next(&self) -> Self {
let parts = self.all();
let parts_iter = parts.iter();

for (index, part) in parts_iter.enumerate() {
if self == part {
return match parts.get(index + 1) {
None => Self::Radians,
Some(unit) => *unit
};
}
}

Self::Radians
}
}

pub trait AsStr {
fn as_str(&self) -> &str;
}

impl AsStr for Part {
fn as_str(&self) -> &str {
match self {
Part::Fundamental => "Фундаментальные",
Part::ArcFunctions => "Обратные функции"
}
}
}

impl AsStr for Unit {
fn as_str(&self) -> &str {
match self {
Unit::Radians => "Радианы",
Unit::Degrees => "Градусы"
}
}
}

pub struct Switcher<T: Clone> {
one: T,
two: T,
what: bool
}

impl<T: Clone> Switcher<T> {
pub fn new(one: T, two: T) -> Self {
Switcher { one, two, what: true }
}

pub fn get(&self) -> T {
match self.what {
true => self.one.clone(),
false => self.two.clone()
}
}

pub fn switch(&mut self) -> T {
self.what = !self.what;
self.get()
}
}

impl<T: Clone + Debug> Debug for Switcher<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let current = self.get();
current.fmt(f)
}
}

impl<T: Clone + Display> Display for Switcher<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let current = self.get();
current.fmt(f)
}
}

pub trait ThemeStrings {
fn sys_name(&self) -> &str;
fn display_name(&self) -> &str;
}

impl ThemeStrings for Switcher<Theme> {
fn sys_name(&self) -> &str {
if self.get() == Theme::Light { "Light" } else { "Dark" }
}

fn display_name(&self) -> &str {
if self.get() == Theme::Light { "Тема: Светлая" } else { "Тема: Тёмная" }
}
}
Loading

0 comments on commit 5d59d46

Please sign in to comment.