Skip to content

Commit f2a0fc4

Browse files
author
saying121
committed
style(submit result): more fancy
1 parent f4f9002 commit f2a0fc4

File tree

10 files changed

+361
-280
lines changed

10 files changed

+361
-280
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ as_ptr_cast_mut = "warn"
248248
# cast_lossless = "warn"
249249
# cast_possible_truncation = "warn"
250250
# cast_possible_wrap = "warn"
251-
cast_precision_loss = "warn"
251+
# cast_precision_loss = "warn"
252252
cast_ptr_alignment = "warn"
253253
cast_sign_loss = "warn"
254254
ptr_as_ptr = "warn"

crates/lcode/src/app/edit.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ratatui::widgets::ScrollbarState;
44
use tui_textarea::{CursorMove, Input, Key, Scrolling, TextArea};
55

66
use super::TuiMode;
7-
use crate::mytui::my_widget::bottons::ButtonState;
7+
use crate::mytui::my_widget::botton::{ButtonState, ButtonStates};
88

99
// tab1 edit
1010
#[derive(Clone)]
@@ -25,7 +25,7 @@ pub struct EditCode<'tab1> {
2525
pub submitting: bool,
2626
pub show_pop_menu: bool,
2727

28-
pub button_state: [ButtonState; 3],
28+
pub button_state: ButtonStates,
2929
pub select_button: usize,
3030

3131
pub submit_res: RunResult,
@@ -269,15 +269,15 @@ impl<'tab1> EditCode<'tab1> {
269269
.position(self.test_vert_scroll);
270270
}
271271
}
272-
else if self.show_submit_res
273-
&& self.submit_vert_scroll < self.submit_row_len.saturating_sub(4)
274-
{
275-
self.submit_vert_scroll = self
276-
.submit_vert_scroll
277-
.saturating_add(1);
278-
self.submit_vert_scroll_state = self
279-
.submit_vert_scroll_state
280-
.position(self.submit_vert_scroll);
272+
else if self.show_submit_res {
273+
if self.submit_vert_scroll < self.submit_row_len.saturating_sub(4) {
274+
self.submit_vert_scroll = self
275+
.submit_vert_scroll
276+
.saturating_add(1);
277+
self.submit_vert_scroll_state = self
278+
.submit_vert_scroll_state
279+
.position(self.submit_vert_scroll);
280+
}
281281
}
282282
else if !self.show_pop_menu
283283
&& self.content_vert_scroll < self.vertical_row_len.saturating_sub(4)
@@ -334,12 +334,12 @@ impl<'tab1> EditCode<'tab1> {
334334
.position(self.submit_hori_scroll);
335335
}
336336
else if self.show_pop_menu {
337-
if self.button_state[self.select_button] != ButtonState::Active {
338-
self.button_state[self.select_button] = ButtonState::Normal;
337+
if self.button_state.states[self.select_button] != ButtonState::Active {
338+
self.button_state.states[self.select_button] = ButtonState::Normal;
339339
}
340340
self.select_button = self.select_button.saturating_sub(1);
341-
if self.button_state[self.select_button] != ButtonState::Active {
342-
self.button_state[self.select_button] = ButtonState::Selected;
341+
if self.button_state.states[self.select_button] != ButtonState::Active {
342+
self.button_state.states[self.select_button] = ButtonState::Selected;
343343
}
344344
}
345345
else {
@@ -367,17 +367,17 @@ impl<'tab1> EditCode<'tab1> {
367367
.position(self.submit_hori_scroll);
368368
}
369369
else if self.show_pop_menu {
370-
if self.button_state[self.select_button] != ButtonState::Active {
371-
self.button_state[self.select_button] = ButtonState::Normal;
370+
if self.button_state.states[self.select_button] != ButtonState::Active {
371+
self.button_state.states[self.select_button] = ButtonState::Normal;
372372
}
373373

374374
self.select_button = self
375375
.select_button
376376
.saturating_add(1)
377377
.min(1);
378378

379-
if self.button_state[self.select_button] != ButtonState::Active {
380-
self.button_state[self.select_button] = ButtonState::Selected;
379+
if self.button_state.states[self.select_button] != ButtonState::Active {
380+
self.button_state.states[self.select_button] = ButtonState::Selected;
381381
}
382382
}
383383
else {

crates/lcode/src/app/impl_app/edit_qs.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use tracing::error;
66

77
use crate::{
88
app::inner::App,
9-
mytui::{my_widget::bottons::ButtonState, myevent::UserEvent},
9+
mytui::{my_widget::botton::ButtonState, myevent::UserEvent},
1010
};
1111

1212
impl<'app_lf> App<'app_lf> {
@@ -34,11 +34,11 @@ impl<'app_lf> App<'app_lf> {
3434
pub fn menu_button_trig(&mut self) -> bool {
3535
match self.edit.select_button {
3636
0 => {
37-
self.edit.button_state[0] = ButtonState::Active;
37+
self.edit.button_state.states[0] = ButtonState::Active;
3838
self.test_code()
3939
},
4040
1 => {
41-
self.edit.button_state[1] = ButtonState::Active;
41+
self.edit.button_state.states[1] = ButtonState::Active;
4242
self.submit_code()
4343
},
4444
_ => false,
@@ -130,7 +130,7 @@ impl<'app_lf> App<'app_lf> {
130130
self.edit.show_pop_menu = false;
131131

132132
self.edit.submitting = false;
133-
self.edit.button_state[0] = ButtonState::Normal;
133+
self.edit.button_state.states[0] = ButtonState::Normal;
134134
self.render();
135135
}
136136
pub fn submit_done(&mut self, res: RunResult) {
@@ -141,7 +141,7 @@ impl<'app_lf> App<'app_lf> {
141141
self.edit.show_pop_menu = false;
142142

143143
self.edit.submitting = false;
144-
self.edit.button_state[1] = ButtonState::Normal;
144+
self.edit.button_state.states[1] = ButtonState::Normal;
145145
self.render();
146146
}
147147
}

crates/lcode/src/mytui/helper.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use ratatui::prelude::*;
1+
use ratatui::{
2+
prelude::*,
3+
widgets::{block::Title, Block, Borders},
4+
};
25

36
/// helper function to create a bottom rect using up certain percentage of the available rect `r`
47
pub(super) fn bottom_rect(percent_x: u16, r: Rect) -> Rect {
@@ -18,6 +21,25 @@ pub(super) fn bottom_rect(percent_x: u16, r: Rect) -> Rect {
1821
)
1922
.split(area[1])[1]
2023
}
24+
pub fn nest_rect(r: Rect, left: u16, right: u16, top: u16, bottom: u16) -> Rect {
25+
let layout = Layout::default()
26+
.direction(Direction::Vertical)
27+
.constraints([
28+
Constraint::Length(top),
29+
Constraint::Min(0),
30+
Constraint::Length(bottom),
31+
])
32+
.split(r);
33+
let layout = Layout::default()
34+
.direction(Direction::Horizontal)
35+
.constraints([
36+
Constraint::Length(left),
37+
Constraint::Min(0),
38+
Constraint::Length(right),
39+
])
40+
.split(layout[1]);
41+
layout[1]
42+
}
2143
/// helper function to create a centered rect using up certain percentage of the available rect `r`
2244
pub(super) fn centered_rect_percent(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
2345
let popup_layout = Layout::default()
@@ -44,3 +66,13 @@ pub(super) fn centered_rect_percent(percent_x: u16, percent_y: u16, r: Rect) ->
4466
)
4567
.split(popup_layout[1])[1]
4668
}
69+
70+
pub fn title_block<'a,T>(title: T) -> Block<'a>
71+
where
72+
T: Into<Line<'a>>,
73+
{
74+
let title = Title::from(title).alignment(Alignment::Center);
75+
Block::default()
76+
.title(title)
77+
.borders(Borders::ALL)
78+
}

crates/lcode/src/mytui/my_widget/bottons.rs crates/lcode/src/mytui/my_widget/botton.rs

+61-50
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,6 @@ use ratatui::{
55
widgets::Widget,
66
};
77

8-
#[derive(Clone)]
9-
#[derive(Debug)]
10-
#[derive(Default)]
11-
#[derive(PartialEq, Eq)]
12-
pub struct Buttons<'a> {
13-
buttons: Vec<Button<'a>>,
14-
states: Vec<ButtonState>,
15-
}
16-
17-
impl<'a> Buttons<'a> {
18-
fn new<T>(labels: Vec<T>) -> Self
19-
where
20-
T: Into<Line<'a>>,
21-
{
22-
let size = labels.len();
23-
let mut buttons = Vec::with_capacity(size);
24-
for i in labels {
25-
buttons.push(Button::new(i));
26-
}
27-
let states = vec![ButtonState::Normal; size];
28-
Self { buttons, states }
29-
}
30-
}
31-
328
#[derive(Clone)]
339
#[derive(Debug)]
3410
#[derive(PartialEq, Eq)]
@@ -38,6 +14,20 @@ pub struct Button<'a> {
3814
state: ButtonState,
3915
}
4016

17+
#[derive(Clone)]
18+
#[derive(Debug)]
19+
pub struct ButtonStates {
20+
pub states: Vec<ButtonState>,
21+
}
22+
23+
impl Default for ButtonStates {
24+
fn default() -> Self {
25+
Self {
26+
states: vec![ButtonState::Selected, ButtonState::Normal],
27+
}
28+
}
29+
}
30+
4131
impl<'a> Button<'a> {
4232
const fn colors(&self) -> (Color, Color, Color, Color) {
4333
let theme = self.theme;
@@ -98,33 +88,54 @@ pub struct Theme {
9888
highlight: Color,
9989
shadow: Color,
10090
}
101-
pub const CYAN: Theme = Theme {
102-
text: Color::Cyan,
103-
background: Color::LightCyan,
104-
shadow: Color::DarkGray,
105-
highlight: Color::Blue,
106-
};
107-
108-
pub const BLUE: Theme = Theme {
109-
text: Color::Rgb(16, 24, 48),
110-
background: Color::Rgb(48, 72, 144),
111-
highlight: Color::Rgb(64, 96, 192),
112-
shadow: Color::Rgb(32, 48, 96),
113-
};
11491

115-
pub const RED: Theme = Theme {
116-
text: Color::Rgb(48, 16, 16),
117-
background: Color::Rgb(144, 48, 48),
118-
highlight: Color::Rgb(192, 64, 64),
119-
shadow: Color::Rgb(96, 32, 32),
120-
};
92+
impl Theme {
93+
pub const fn test_color() -> Self {
94+
let (r, g, b) = (21, 21, 16);
95+
Self {
96+
text: Color::Rgb(r, g, b),
97+
shadow: Color::Rgb(r * 2, g * 2, b * 2),
98+
background: Color::Rgb(r * 3, g * 3, b * 3),
99+
highlight: Color::Rgb(r * 6, g * 6, b * 6),
100+
}
101+
}
102+
pub const fn blue() -> Self {
103+
let (r, g, b) = (16, 24, 48);
104+
Self {
105+
text: Color::Rgb(r, g, b),
106+
shadow: Color::Rgb(r * 2, g * 2, b * 2),
107+
background: Color::Rgb(r * 3, g * 3, b * 3),
108+
highlight: Color::Rgb(r * 5, g * 5, b * 5),
109+
}
110+
}
121111

122-
pub const GREEN: Theme = Theme {
123-
text: Color::Rgb(16, 48, 16),
124-
background: Color::Rgb(48, 144, 48),
125-
highlight: Color::Rgb(64, 192, 64),
126-
shadow: Color::Rgb(32, 96, 32),
127-
};
112+
pub const fn red() -> Self {
113+
let (r, g, b) = (48, 16, 16);
114+
Self {
115+
text: Color::Rgb(r, g, b),
116+
shadow: Color::Rgb(r * 2, g * 2, b * 2),
117+
background: Color::Rgb(r * 3, g * 3, b * 3),
118+
highlight: Color::Rgb(r * 4, g * 4, b * 4),
119+
}
120+
}
121+
pub const fn green() -> Self {
122+
let (r, g, b) = (16, 48, 16);
123+
Self {
124+
text: Color::Rgb(r, g, b),
125+
shadow: Color::Rgb(r * 2, g * 2, b * 2),
126+
background: Color::Rgb(r * 3, g * 3, b * 3),
127+
highlight: Color::Rgb(r * 4, g * 4, b * 4),
128+
}
129+
}
130+
pub const fn submit_color() -> Self {
131+
Self {
132+
text: Color::Blue,
133+
background: Color::Reset,
134+
highlight: Color::Rgb(64, 96, 192),
135+
shadow: Color::Rgb(32, 48, 96),
136+
}
137+
}
138+
}
128139

129140
impl<'a> Button<'a> {
130141
pub fn new<T>(label: T) -> Self
@@ -133,7 +144,7 @@ impl<'a> Button<'a> {
133144
{
134145
Self {
135146
label: label.into(),
136-
theme: BLUE,
147+
theme: Theme::blue(),
137148
state: ButtonState::Normal,
138149
}
139150
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// pub mod as_image;
2-
pub mod bottons;
2+
pub mod botton;

0 commit comments

Comments
 (0)