use i32 for negative values calculation

This commit is contained in:
yggverse 2024-10-16 18:03:20 +03:00
parent b3b0892d62
commit 42e91cdb7c
4 changed files with 18 additions and 21 deletions

View File

@ -40,10 +40,11 @@ impl Default {
let response = response.clone();
move |_, _| {
control.update(match size_limit {
Some(limit_value) => Some(
limit_value
- (base.to_string_partial(UriHideFlags::QUERY).len()
+ Uri::escape_string(response.text().as_str(), None, false).len()),
Some(limit) => Some(
limit as i32
- (base.to_string_partial(UriHideFlags::QUERY).len() as i32
+ Uri::escape_string(response.text().as_str(), None, false).len()
as i32),
),
None => None,
});

View File

@ -10,7 +10,7 @@ use gtk::Box;
use std::sync::Arc;
pub struct Control {
limit: Arc<Left>,
left: Arc<Left>,
send: Arc<Send>,
widget: Arc<Widget>,
}
@ -19,25 +19,21 @@ impl Control {
// Construct
pub fn new_arc() -> Arc<Self> {
// Init components
let limit = Left::new_arc();
let left = Left::new_arc();
let send = Send::new_arc();
// Init widget
let widget = Widget::new_arc(limit.gobject(), send.gobject());
let widget = Widget::new_arc(left.gobject(), send.gobject());
// Return activated struct
Arc::new(Self {
limit,
send,
widget,
})
Arc::new(Self { left, send, widget })
}
// Actions
pub fn update(&self, left: Option<usize>) {
pub fn update(&self, chars_left: Option<i32>) {
// Update children components
self.limit.update(left);
self.send.update(match left {
self.left.update(chars_left);
self.send.update(match chars_left {
Some(value) => value > 0,
None => false,
});

View File

@ -20,8 +20,8 @@ impl Left {
}
// Actions
pub fn update(&self, left: Option<usize>) {
self.widget.update(left);
pub fn update(&self, chars_left: Option<i32>) {
self.widget.update(chars_left);
}
// Getters

View File

@ -14,17 +14,17 @@ impl Widget {
}
// Actions
pub fn update(&self, left: Option<usize>) {
match left {
pub fn update(&self, chars_left: Option<i32>) {
match chars_left {
Some(value) => {
// Update color on limit reached
// Update color on chars left reached
self.gobject
.set_css_classes(&[if value > 0 { "success" } else { "error" }]); // @TODO add warning step?
// Update text
self.gobject.set_label(&value.to_string());
// Toggle visibility if limit provided
// Toggle visibility on chars left provided
self.gobject.set_visible(true);
}
None => self.gobject.set_visible(false),