Skip to content

Commit

Permalink
feat: expand text input to 4 lines when focused
Browse files Browse the repository at this point in the history
  • Loading branch information
yusdacra committed Jan 25, 2022
1 parent b4154d6 commit 7e6370f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ops::Not;

use client::Client;
use eframe::{
egui::{self, Color32, FontData, FontDefinitions, Ui, Vec2},
egui::{self, vec2, Color32, FontData, FontDefinitions, Ui, Vec2},
epi,
};

Expand Down Expand Up @@ -76,7 +76,7 @@ impl App {
#[inline(always)]
fn view_bottom_panel(&mut self, ui: &mut Ui, frame: &epi::Frame) {
ui.horizontal_top(|ui| {
ui.style_mut().spacing.item_spacing = egui::vec2(2.0, 0.0);
ui.style_mut().spacing.item_spacing = vec2(2.0, 0.0);

self.view_connection_status(ui);

Expand Down
29 changes: 21 additions & 8 deletions src/screen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use client::{
smol_str::SmolStr,
AHashMap, AHashSet, FetchEvent,
};
use eframe::egui::{Color32, Event, RichText, Vec2};
use eframe::egui::{vec2, Color32, Event, RichText, Vec2};

use crate::{
config::BgImage,
Expand Down Expand Up @@ -127,7 +127,11 @@ pub struct Screen {
loading_attachment: AHashMap<FileId, AtomBool>,
/// current guild / channel
current: CurrentIds,
/// main message composer
composer: EasyMarkEditor,
/// was the main composer focused in last frame
is_composer_focused: bool,
/// composer used for editing messages
edit_message_composer: EasyMarkEditor,
/// whether to scroll to bottom on next frame
scroll_to_bottom: bool,
Expand Down Expand Up @@ -829,12 +833,12 @@ impl Screen {
}
}

fn view_composer(&mut self, state: &mut State, ui: &mut Ui, ctx: &egui::Context) {
fn view_composer(&mut self, state: &mut State, ui: &mut Ui, ctx: &egui::Context, desired_lines: usize) {
let Some((guild_id, channel_id)) = self.current.channel() else { return };

let text_edit = self
.composer
.desired_rows(1)
.desired_rows(desired_lines)
.desired_width(ui.available_width() - 36.0)
.hint_text("Enter message...")
.editor_ui(ui);
Expand Down Expand Up @@ -874,6 +878,8 @@ impl Screen {
spawn_client_fut!(state, |client| client.send_typing(guild_id, channel_id).await);
}
}

self.is_composer_focused = text_edit.has_focus();
}

fn view_uploading_attachments(&mut self, state: &State, ui: &mut Ui) {
Expand Down Expand Up @@ -1213,13 +1219,20 @@ impl Screen {
ui.horizontal(|ui| self.view_uploading_attachments(state, ui));
});
}
ui.allocate_ui([ui.available_width(), ui.available_height() - 38.0].into(), |ui| {
self.view_messages(state, ui);
});

let (desired_lines, extra_offset) = self.is_composer_focused.then(|| (4, 0.0)).unwrap_or((1, 14.0));
let desired_height = (desired_lines as f32 * ui.style().spacing.interact_size.y) + extra_offset;
ui.allocate_ui(
vec2(ui.available_width(), ui.available_height() - desired_height),
|ui| {
self.view_messages(state, ui);
},
);

ui.group_filled().show(ui, |ui| {
self.view_typing_members(state, ui);
ui.horizontal(|ui| {
self.view_composer(state, ui, ctx);
self.view_composer(state, ui, ctx, desired_lines);
self.view_upload_button(state, ui);
});
});
Expand Down Expand Up @@ -1304,7 +1317,7 @@ impl AppScreen for Screen {
let size = size * 0.2;
ImageBg::new(texid, size)
.tint(Color32::WHITE.linear_multiply(0.05))
.offset(ui.available_size() - (size * 0.8) - egui::vec2(75.0, 0.0))
.offset(ui.available_size() - (size * 0.8) - vec2(75.0, 0.0))
.show(ui, |ui| show_main(state, ui));
}
_ => show_main(state, ui),
Expand Down

0 comments on commit 7e6370f

Please sign in to comment.