Skip to content

Commit

Permalink
Support light theme
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier committed Jul 10, 2024
1 parent 5d38249 commit 70f6ec6
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "GPL-3.0"
name = "air"
readme = "README.md"
repository = "https://github.com/hack-ink/air"
version = "0.1.0"
version = "0.2.0"

[package.metadata.bundle]
icon = ["asset/icon.png"]
Expand Down
File renamed without changes
1 change: 1 addition & 0 deletions asset/copied-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions asset/copy-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions asset/copy-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion asset/copy.svg

This file was deleted.

1 change: 1 addition & 0 deletions asset/retry-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions asset/retry-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion asset/retry.svg

This file was deleted.

6 changes: 4 additions & 2 deletions src/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ pub fn launch() -> Result<()> {
.unwrap(),
)
.with_inner_size((720., 360.))
.with_min_inner_size((720., 360.))
.with_transparent(true),
.with_min_inner_size((720., 360.)),
// TODO?: transparent window.
// .with_transparent(true),
follow_system_theme: true,
..Default::default()
},
Box::new(|c| Ok(Box::new(AiR::new(&c.egui_ctx).unwrap()))),
Expand Down
2 changes: 2 additions & 0 deletions src/os.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[cfg(target_os = "macos")] mod macos;
#[cfg(all(unix, not(target_os = "macos")))] mod unix;
#[cfg(target_os = "windows")] mod windows;

#[derive(Debug)]
pub struct Os;
Empty file added src/os/unix.rs
Empty file.
Empty file added src/os/windows.rs
Empty file.
9 changes: 5 additions & 4 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ trait UiT {

#[derive(Debug, Default)]
pub struct Uis {
pub focused_panel: Panel,
pub chat: Chat,
pub setting: Setting,
focused_panel: Panel,
chat: Chat,
setting: Setting,
}
impl Uis {
pub fn new() -> Self {
Expand All @@ -25,8 +25,9 @@ impl Uis {

pub fn draw(&mut self, mut ctx: AiRContext) {
CentralPanel::default()
// TODO:? transparent window.
// FIXME: it looks like there some invalid cache.
.frame(util::transparent_frame(ctx.egui_ctx))
// .frame(util::transparent_frame(ctx.egui_ctx))
.show(ctx.egui_ctx, |ui| {
ui.horizontal(|ui| {
ui.selectable_value(&mut self.focused_panel, Panel::Chat, Panel::Chat.name());
Expand Down
86 changes: 59 additions & 27 deletions src/ui/panel/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ use super::super::UiT;
use crate::{
air::AiRContext,
component::{openai::Model, util},
widget,
};

#[derive(Debug, Default)]
pub struct Chat {
pub input: String,
pub output: String,
pub shortcut: ShortcutWidget,
input: String,
output: String,
shortcut: ShortcutWidget,
}
impl UiT for Chat {
fn draw(&mut self, ui: &mut Ui, ctx: &mut AiRContext) {
let is_chatting = ctx.services.is_chatting();
let dark_mode = ui.visuals().dark_mode;
let size = ui.available_size();
let is_chatting = ctx.services.is_chatting();

ScrollArea::vertical().id_source("Input").max_height((size.y - 50.) / 2.).show(ui, |ui| {
let input = ui.add_sized(
Expand Down Expand Up @@ -88,7 +90,7 @@ impl UiT for Chat {
} else {
// TODO: change retry to send.
// TODO: the state will not be synced if previous action is triggered by hotkey.
if ui.add(self.shortcut.retry.clone()).clicked() {
if ui.add(self.shortcut.retry.img(dark_mode)).clicked() {
ctx.services.chat.send((
ctx.components.setting.general.active_func.basic(),
self.input.clone(),
Expand All @@ -97,15 +99,15 @@ impl UiT for Chat {
}
}
if !self.shortcut.copy.triggered {
if ui.add(self.shortcut.copy.copy_img.clone()).clicked() {
if ui.add(self.shortcut.copy.copy_img(dark_mode)).clicked() {
self.shortcut.copy.triggered = true;
ctx.components
.clipboard
.set_text(&self.output)
.expect("clipboard must be available");
}
} else {
ui.add(self.shortcut.copy.copied_img.clone());
ui.add(self.shortcut.copy.copied_img(dark_mode));
}
});
});
Expand All @@ -124,37 +126,67 @@ impl UiT for Chat {
}
}

#[derive(Debug)]
pub struct ShortcutWidget {
retry: Image<'static>,
#[derive(Debug, Default)]
struct ShortcutWidget {
copy: CopyWidget,
retry: RetryWidget,
}
// TODO: https://github.com/emilk/egui/issues/3453.
#[derive(Debug)]
struct CopyWidget {
copy_img_l: Image<'static>,
copy_img_d: Image<'static>,
copied_img_l: Image<'static>,
copied_img_d: Image<'static>,
triggered: bool,
}
impl Default for ShortcutWidget {
impl CopyWidget {
fn copy_img(&self, dark_mode: bool) -> Image<'static> {
if dark_mode {
self.copy_img_d.clone()
} else {
self.copy_img_l.clone()
}
}

fn copied_img(&self, dark_mode: bool) -> Image<'static> {
if dark_mode {
self.copied_img_d.clone()
} else {
self.copied_img_l.clone()
}
}
}
impl Default for CopyWidget {
fn default() -> Self {
Self {
retry: Image::new(include_image!("../../../asset/retry.svg"))
.max_size((16., 16.).into())
.sense(Sense::click()),
copy: Default::default(),
copy_img_d: widget::image_button(include_image!("../../../asset/copy-dark.svg")),
copy_img_l: widget::image_button(include_image!("../../../asset/copy-light.svg")),
copied_img_d: widget::image_button(include_image!("../../../asset/copied-dark.svg")),
copied_img_l: widget::image_button(include_image!("../../../asset/copied-light.svg")),
triggered: false,
}
}
}
// TODO: https://github.com/emilk/egui/issues/3453.
#[derive(Debug)]
pub struct CopyWidget {
copy_img: Image<'static>,
copied_img: Image<'static>,
triggered: bool,
struct RetryWidget {
retry_img_d: Image<'static>,
retry_img_l: Image<'static>,
}
impl Default for CopyWidget {
impl RetryWidget {
fn img(&self, dark_mode: bool) -> Image<'static> {
if dark_mode {
self.retry_img_d.clone()
} else {
self.retry_img_l.clone()
}
}
}
impl Default for RetryWidget {
fn default() -> Self {
Self {
copy_img: Image::new(include_image!("../../../asset/copy.svg"))
.max_size((16., 16.).into())
.sense(Sense::click()),
copied_img: Image::new(include_image!("../../../asset/check.svg"))
.max_size((16., 16.).into()),
triggered: false,
retry_img_d: widget::image_button(include_image!("../../../asset/retry-dark.svg")),
retry_img_l: widget::image_button(include_image!("../../../asset/retry-light.svg")),
}
}
}
2 changes: 1 addition & 1 deletion src/ui/panel/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{air::AiRContext, widget};

#[derive(Debug, Default)]
pub struct Setting {
pub api_key: ApiKeyWidget,
api_key: ApiKeyWidget,
}
impl Setting {
fn set_font_sizes(&self, ctx: &AiRContext) {
Expand Down
2 changes: 2 additions & 0 deletions src/ui/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// crates.io
use eframe::egui::*;

// TODO?: transparent window.
#[allow(unused)]
pub fn transparent_frame(ctx: &Context) -> Frame {
Frame::central_panel(&ctx.style()).fill(Color32::TRANSPARENT)
}
7 changes: 7 additions & 0 deletions src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ pub fn toggle(on: &mut bool) -> impl Widget + '_ {
response
}
}

pub fn image_button<S>(src: S) -> Image<'static>
where
S: Into<ImageSource<'static>>,
{
Image::new(src).max_size((16., 16.).into()).sense(Sense::click())
}

0 comments on commit 70f6ec6

Please sign in to comment.