Skip to content

Commit

Permalink
Improve UIs
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier committed Jul 14, 2024
1 parent 80c0be5 commit 7b95910
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 153 deletions.
2 changes: 1 addition & 1 deletion README-zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ AiR 是非母语者的绝佳助手,能够自动修正错误并润色语言,

## 功能亮点
### 用户界面
<div align="center"><img src="demo/ui-1.png" width="49.7%"/> <img src="demo/ui-2.png" width="49.7%"/></div>
<div align="center"><img src="demo/ui-0.png" width="49.7%"/> <img src="demo/ui-1.png" width="49.7%"/></div>

### 直接重写
![rewrite-directly](demo/rewrite-directly.gif)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ More awesome features are on the horizon!

## Feature Highlights
### UIs
<div align="center"><img src="demo/ui-1.png" width="49.7%"/> <img src="demo/ui-2.png" width="49.7%"/></div>
<div align="center"><img src="demo/ui-0.png" width="49.7%"/> <img src="demo/ui-1.png" width="49.7%"/></div>

### Rewrite Directly
![rewrite-directly](demo/rewrite-directly.gif)
Expand Down
Binary file added demo/ui-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified demo/ui-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed demo/ui-2.png
Binary file not shown.
13 changes: 8 additions & 5 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ impl Uis {
TopBottomPanel::top("Top Panel").show(ctx.egui_ctx, |ui| self.tabs.draw(ui, bar_h));
// Main body.
CentralPanel::default().show(ctx.egui_ctx, |ui| match self.tabs.focused_tab {
Panel::Chat => self.chat.draw(&mut ctx, ui, bar_h),
Panel::Setting => self.setting.draw(&mut ctx, ui, bar_h),
Panel::Chat => {
self.chat.draw(&mut ctx, ui, bar_h);

// Status bar.
TopBottomPanel::bottom("Bottom Panel")
.show(ctx.egui_ctx, |ui| self.status.draw(&mut ctx, ui, bar_h, &self.chat));
},
Panel::Setting => self.setting.draw(&mut ctx, ui),
});
// Status bar.
TopBottomPanel::bottom("Bottom Panel")
.show(ctx.egui_ctx, |ui| self.status.draw(&mut ctx, ui, bar_h, &self.chat));
}
}

Expand Down
284 changes: 138 additions & 146 deletions src/ui/panel/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,177 +14,169 @@ pub struct Setting {
hotkey_listeners: [HotkeyListener; 4],
}
impl Setting {
pub fn draw(&mut self, ctx: &mut AiRContext, ui: &mut Ui, bar_height: f32) {
ui.vertical(|ui| {
ui.set_height(ui.available_size().y - bar_height);

ScrollArea::vertical().id_source("Setting").auto_shrink(false).show(ui, |ui| {
let mut chat_need_reload = false;

ui.collapsing("General", |ui| {
Grid::new("General").num_columns(2).show(ui, |ui| {
ui.label("Font Size");
ui.horizontal(|ui| {
if ui
.add(
Slider::new(
&mut ctx.components.setting.general.font_size,
9_f32..=16.,
)
.step_by(1.)
.fixed_decimals(0),
)
.changed()
{
super::super::set_font_size(
ctx.egui_ctx,
ctx.components.setting.general.font_size,
);
}
});

ui.end_row();
ui.label("Hide on Lost Focus");
pub fn draw(&mut self, ctx: &mut AiRContext, ui: &mut Ui) {
ScrollArea::vertical().id_source("Setting").auto_shrink(false).show(ui, |ui| {
let mut chat_need_reload = false;

ui.collapsing("General", |ui| {
Grid::new("General").num_columns(2).show(ui, |ui| {
ui.label("Font Size");
ui.horizontal(|ui| {
if ui
.add(widget::toggle(
&mut ctx.components.setting.general.hide_on_lost_focus,
))
.add(
Slider::new(
&mut ctx.components.setting.general.font_size,
9_f32..=16.,
)
.step_by(1.)
.fixed_decimals(0),
)
.changed()
{
ctx.state.general.hide_on_lost_focus.store(
ctx.components.setting.general.hide_on_lost_focus,
Ordering::Relaxed,
super::super::set_font_size(
ctx.egui_ctx,
ctx.components.setting.general.font_size,
);
};

ui.end_row();
// TODO: move to a better place.
ui.add(widget::combo_box(
"Active Function",
&mut ctx.components.setting.general.active_func,
));
}
});
});

ui.collapsing("AI", |ui| {
Grid::new("AI").num_columns(2).show(ui, |ui| {
ui.label("API Base");
// The available size only works after there is an existing element.
let size = ui.available_size();

ui.horizontal(|ui| {
chat_need_reload |= ui
.add_sized(
size,
TextEdit::singleline(&mut ctx.components.setting.ai.api_base),
)
.changed();
});

ui.end_row();
ui.label("API Key");
ui.horizontal(|ui| {
chat_need_reload |= ui
.add_sized(
size,
TextEdit::singleline(&mut ctx.components.setting.ai.api_key)
.password(self.api_key.visibility),
)
.changed();
ui.end_row();
ui.label("Hide on Lost Focus");
if ui
.add(widget::toggle(&mut ctx.components.setting.general.hide_on_lost_focus))
.changed()
{
ctx.state.general.hide_on_lost_focus.store(
ctx.components.setting.general.hide_on_lost_focus,
Ordering::Relaxed,
);
};

ui.end_row();
// TODO: move to a better place.
ui.add(widget::combo_box(
"Active Function",
&mut ctx.components.setting.general.active_func,
));
});
});

if ui.button(&self.api_key.label).clicked() {
self.api_key.clicked();
}
});
ui.collapsing("AI", |ui| {
Grid::new("AI").num_columns(2).show(ui, |ui| {
ui.label("API Base");
// The available size only works after there is an existing element.
let size = ui.available_size();

ui.end_row();
ui.horizontal(|ui| {
chat_need_reload |= ui
.add(widget::combo_box("Model", &mut ctx.components.setting.ai.model))
.add_sized(
size,
TextEdit::singleline(&mut ctx.components.setting.ai.api_base),
)
.changed();
});

ui.end_row();
ui.label("Temperature");
ui.spacing_mut().slider_width = size.x;
ui.end_row();
ui.label("API Key");
ui.horizontal(|ui| {
chat_need_reload |= ui
.add(
Slider::new(&mut ctx.components.setting.ai.temperature, 0_f32..=2.)
.fixed_decimals(1)
.step_by(0.1),
.add_sized(
size,
TextEdit::singleline(&mut ctx.components.setting.ai.api_key)
.password(self.api_key.visibility),
)
.changed();
});
});

ui.collapsing("Translation", |ui| {
Grid::new("Translation").num_columns(2).show(ui, |ui| {
// TODO: A and B should be mutually exclusive.
for (l, c) in [
("Language A", &mut ctx.components.setting.chat.translation.a),
("Language B", &mut ctx.components.setting.chat.translation.b),
] {
chat_need_reload |= ui.add(widget::combo_box(l, c)).changed();
ui.end_row();
if ui.button(&self.api_key.label).clicked() {
self.api_key.clicked();
}
});

ui.end_row();
chat_need_reload |= ui
.add(widget::combo_box("Model", &mut ctx.components.setting.ai.model))
.changed();

ui.end_row();
ui.label("Temperature");
ui.spacing_mut().slider_width = size.x;
chat_need_reload |= ui
.add(
Slider::new(&mut ctx.components.setting.ai.temperature, 0_f32..=2.)
.fixed_decimals(1)
.step_by(0.1),
)
.changed();
});
});

ui.collapsing("Hotkey", |ui| {
Grid::new("Hotkey").num_columns(2).show(ui, |ui| {
if self
.hotkey_listeners
.iter_mut()
.zip(
[
("Rewrite", &mut ctx.components.setting.hotkeys.rewrite),
(
"Rewrite Directly",
&mut ctx.components.setting.hotkeys.rewrite_directly,
),
("Translate", &mut ctx.components.setting.hotkeys.translate),
(
"Translate Directly",
&mut ctx.components.setting.hotkeys.translate_directly,
),
]
.iter_mut(),
)
.fold(false, |mut changed, (kl, (l, hk))| {
changed |= kl.listen(ui, l, hk);
ui.collapsing("Translation", |ui| {
Grid::new("Translation").num_columns(2).show(ui, |ui| {
// TODO: A and B should be mutually exclusive.
for (l, c) in [
("Language A", &mut ctx.components.setting.chat.translation.a),
("Language B", &mut ctx.components.setting.chat.translation.b),
] {
chat_need_reload |= ui.add(widget::combo_box(l, c)).changed();
ui.end_row();
}
});
});

ui.end_row();
ui.collapsing("Hotkey", |ui| {
Grid::new("Hotkey").num_columns(2).show(ui, |ui| {
if self
.hotkey_listeners
.iter_mut()
.zip(
[
("Rewrite", &mut ctx.components.setting.hotkeys.rewrite),
(
"Rewrite Directly",
&mut ctx.components.setting.hotkeys.rewrite_directly,
),
("Translate", &mut ctx.components.setting.hotkeys.translate),
(
"Translate Directly",
&mut ctx.components.setting.hotkeys.translate_directly,
),
]
.iter_mut(),
)
.fold(false, |mut changed, (kl, (l, hk))| {
changed |= kl.listen(ui, l, hk);

changed
}) {
ctx.services.hotkey.renew(&ctx.components.setting.hotkeys);
}
});
});
ui.end_row();

ui.collapsing("Development", |ui| {
Grid::new("Development").num_columns(2).show(ui, |ui| {
if ui
.add(widget::combo_box(
"Log Level",
&mut ctx.components.setting.development.log_level,
))
.changed()
{
ctx.state
.development
.reload_log_filter(
ctx.components.setting.development.log_level.clone().into(),
)
.expect("reload must succeed");
}
});
changed
}) {
ctx.services.hotkey.renew(&ctx.components.setting.hotkeys);
}
});
});

if chat_need_reload {
ctx.services
.chat
.renew(&ctx.components.setting.ai, &ctx.components.setting.chat);
}
ui.collapsing("Development", |ui| {
Grid::new("Development").num_columns(2).show(ui, |ui| {
if ui
.add(widget::combo_box(
"Log Level",
&mut ctx.components.setting.development.log_level,
))
.changed()
{
ctx.state
.development
.reload_log_filter(
ctx.components.setting.development.log_level.clone().into(),
)
.expect("reload must succeed");
}
});
});

if chat_need_reload {
ctx.services.chat.renew(&ctx.components.setting.ai, &ctx.components.setting.chat);
}
});
}
}
Expand Down

0 comments on commit 7b95910

Please sign in to comment.