Let note module own the info on how to render a note which is used by themes

This commit is contained in:
Bu5hm4nn 2023-03-13 16:50:35 -06:00
parent e9aaabc416
commit 2fee804543
6 changed files with 67 additions and 66 deletions

View File

@ -7,6 +7,7 @@ use egui::{Context, Frame, RichText, ScrollArea, SelectableLabel, Ui, Vec2};
use nostr_types::Id; use nostr_types::Id;
mod note; mod note;
pub use note::NoteRenderData;
mod post; mod post;
struct FeedNoteParams { struct FeedNoteParams {

View File

@ -3,7 +3,6 @@ use crate::comms::ToOverlordMessage;
use crate::feed::FeedKind; use crate::feed::FeedKind;
use crate::globals::{Globals, GLOBALS}; use crate::globals::{Globals, GLOBALS};
use crate::people::DbPerson; use crate::people::DbPerson;
use crate::ui::theme::PostProperties;
use crate::ui::widgets::CopyButton; use crate::ui::widgets::CopyButton;
use crate::ui::{GossipUi, Page}; use crate::ui::{GossipUi, Page};
use crate::AVATAR_SIZE_F32; use crate::AVATAR_SIZE_F32;
@ -57,6 +56,22 @@ impl NoteData {
} }
} }
pub struct NoteRenderData {
pub height: f32,
/// Post height
pub is_new: bool,
/// This message is the focus of the view (formerly called is_main_event)
pub is_focused: bool,
/// This message is part of a thread
pub is_thread: bool,
/// Is this the first post in the display?
pub is_first: bool,
/// Is this the last post in the display
pub is_last: bool,
/// Position in the thread, focused message = 0
pub thread_position: i32,
}
pub(super) fn render_note( pub(super) fn render_note(
app: &mut GossipUi, app: &mut GossipUi,
ctx: &Context, ctx: &Context,
@ -102,7 +117,7 @@ pub(super) fn render_note(
} else { } else {
&0.0 &0.0
}; };
let post_properties = PostProperties { let post_properties = NoteRenderData {
height: *height, height: *height,
is_new, is_new,
is_thread: threaded, is_thread: threaded,

View File

@ -1,4 +1,4 @@
use super::{FeedProperties, PostProperties, ThemeDef}; use super::{FeedProperties, NoteRenderData, ThemeDef};
use crate::ui::HighlightType; use crate::ui::HighlightType;
use eframe::egui::style::{Selection, WidgetVisuals, Widgets}; use eframe::egui::style::{Selection, WidgetVisuals, Widgets};
use eframe::egui::{FontDefinitions, Margin, RichText, Style, TextFormat, TextStyle, Visuals}; use eframe::egui::{FontDefinitions, Margin, RichText, Style, TextFormat, TextStyle, Visuals};
@ -378,15 +378,15 @@ impl ThemeDef for ClassicTheme {
fn feed_scroll_stroke(_dark_mode: bool, _feed: &FeedProperties) -> Stroke { fn feed_scroll_stroke(_dark_mode: bool, _feed: &FeedProperties) -> Stroke {
Stroke::NONE Stroke::NONE
} }
fn feed_post_separator_stroke(dark_mode: bool, _post: &PostProperties) -> Stroke { fn feed_post_separator_stroke(dark_mode: bool, _post: &NoteRenderData) -> Stroke {
if dark_mode { if dark_mode {
Stroke::new(1.0, Color32::from_gray(72)) Stroke::new(1.0, Color32::from_gray(72))
} else { } else {
Stroke::new(1.0, Color32::from_gray(192)) Stroke::new(1.0, Color32::from_gray(192))
} }
} }
fn feed_post_outer_indent(_ui: &mut eframe::egui::Ui, _post: &PostProperties) {} fn feed_post_outer_indent(_ui: &mut eframe::egui::Ui, _post: &NoteRenderData) {}
fn feed_post_inner_indent(ui: &mut eframe::egui::Ui, post: &PostProperties) { fn feed_post_inner_indent(ui: &mut eframe::egui::Ui, post: &NoteRenderData) {
if post.is_thread { if post.is_thread {
let space = 100.0 * (10.0 - (1000.0 / (post.thread_position as f32 + 100.0))); let space = 100.0 * (10.0 - (1000.0 / (post.thread_position as f32 + 100.0)));
ui.add_space(space); ui.add_space(space);
@ -399,7 +399,7 @@ impl ThemeDef for ClassicTheme {
} }
} }
} }
fn feed_frame_inner_margin(_post: &PostProperties) -> Margin { fn feed_frame_inner_margin(_post: &NoteRenderData) -> Margin {
Margin { Margin {
left: 0.0, left: 0.0,
top: 4.0, top: 4.0,
@ -407,16 +407,16 @@ impl ThemeDef for ClassicTheme {
bottom: 0.0, bottom: 0.0,
} }
} }
fn feed_frame_outer_margin(_post: &PostProperties) -> Margin { fn feed_frame_outer_margin(_post: &NoteRenderData) -> Margin {
Margin::default() Margin::default()
} }
fn feed_frame_rounding(_post: &PostProperties) -> Rounding { fn feed_frame_rounding(_post: &NoteRenderData) -> Rounding {
Rounding::default() Rounding::default()
} }
fn feed_frame_shadow(_dark_mode: bool, _post: &PostProperties) -> Shadow { fn feed_frame_shadow(_dark_mode: bool, _post: &NoteRenderData) -> Shadow {
Shadow::default() Shadow::default()
} }
fn feed_frame_fill(dark_mode: bool, post: &PostProperties) -> Color32 { fn feed_frame_fill(dark_mode: bool, post: &NoteRenderData) -> Color32 {
if post.is_new { if post.is_new {
if dark_mode { if dark_mode {
Color32::from_rgb(60, 0, 0) Color32::from_rgb(60, 0, 0)
@ -431,7 +431,7 @@ impl ThemeDef for ClassicTheme {
} }
} }
} }
fn feed_frame_stroke(_dark_mode: bool, _post: &PostProperties) -> Stroke { fn feed_frame_stroke(_dark_mode: bool, _post: &NoteRenderData) -> Stroke {
Stroke::NONE Stroke::NONE
} }

View File

@ -1,4 +1,4 @@
use super::{FeedProperties, PostProperties, ThemeDef}; use super::{FeedProperties, NoteRenderData, ThemeDef};
use crate::ui::HighlightType; use crate::ui::HighlightType;
use eframe::egui::style::{Selection, WidgetVisuals, Widgets}; use eframe::egui::style::{Selection, WidgetVisuals, Widgets};
use eframe::egui::{ use eframe::egui::{
@ -381,11 +381,11 @@ impl ThemeDef for DefaultTheme {
fn feed_scroll_stroke(_dark_mode: bool, _feed: &FeedProperties) -> Stroke { fn feed_scroll_stroke(_dark_mode: bool, _feed: &FeedProperties) -> Stroke {
Stroke::NONE Stroke::NONE
} }
fn feed_post_separator_stroke(_dark_mode: bool, _post: &PostProperties) -> Stroke { fn feed_post_separator_stroke(_dark_mode: bool, _post: &NoteRenderData) -> Stroke {
Stroke::NONE Stroke::NONE
} }
fn feed_post_outer_indent(_ui: &mut eframe::egui::Ui, _post: &PostProperties) {} fn feed_post_outer_indent(_ui: &mut eframe::egui::Ui, _post: &NoteRenderData) {}
fn feed_post_inner_indent(ui: &mut eframe::egui::Ui, post: &PostProperties) { fn feed_post_inner_indent(ui: &mut eframe::egui::Ui, post: &NoteRenderData) {
if post.is_thread { if post.is_thread {
if post.thread_position > 0 { if post.thread_position > 0 {
let space = 150.0 * (10.0 - (1000.0 / (post.thread_position as f32 + 100.0))); let space = 150.0 * (10.0 - (1000.0 / (post.thread_position as f32 + 100.0)));
@ -421,7 +421,7 @@ impl ThemeDef for DefaultTheme {
} }
} }
} }
fn feed_frame_inner_margin(_post: &PostProperties) -> Margin { fn feed_frame_inner_margin(_post: &NoteRenderData) -> Margin {
Margin { Margin {
left: 10.0, left: 10.0,
top: 14.0, top: 14.0,
@ -429,16 +429,16 @@ impl ThemeDef for DefaultTheme {
bottom: 6.0, bottom: 6.0,
} }
} }
fn feed_frame_outer_margin(_post: &PostProperties) -> Margin { fn feed_frame_outer_margin(_post: &NoteRenderData) -> Margin {
Margin::symmetric(0.0, 0.0) Margin::symmetric(0.0, 0.0)
} }
fn feed_frame_rounding(_post: &PostProperties) -> Rounding { fn feed_frame_rounding(_post: &NoteRenderData) -> Rounding {
Rounding::same(4.0) Rounding::same(4.0)
} }
fn feed_frame_shadow(_dark_mode: bool, _post: &PostProperties) -> Shadow { fn feed_frame_shadow(_dark_mode: bool, _post: &NoteRenderData) -> Shadow {
Shadow::default() Shadow::default()
} }
fn feed_frame_fill(dark_mode: bool, post: &PostProperties) -> Color32 { fn feed_frame_fill(dark_mode: bool, post: &NoteRenderData) -> Color32 {
if post.is_focused { if post.is_focused {
if dark_mode { if dark_mode {
Color32::from_rgb(16, 23, 33) Color32::from_rgb(16, 23, 33)
@ -459,7 +459,7 @@ impl ThemeDef for DefaultTheme {
} }
} }
} }
fn feed_frame_stroke(_dark_mode: bool, _post: &PostProperties) -> Stroke { fn feed_frame_stroke(_dark_mode: bool, _post: &NoteRenderData) -> Stroke {
Stroke::NONE Stroke::NONE
} }

View File

@ -1,4 +1,5 @@
use super::HighlightType; use super::HighlightType;
use super::feed::NoteRenderData;
use eframe::egui::{ use eframe::egui::{
Color32, Context, FontData, FontDefinitions, FontTweak, Margin, Rounding, Stroke, Style, Color32, Context, FontData, FontDefinitions, FontTweak, Margin, Rounding, Stroke, Style,
TextFormat, TextStyle, Ui, TextFormat, TextStyle, Ui,
@ -42,22 +43,6 @@ pub struct FeedProperties {
pub is_thread: bool, pub is_thread: bool,
} }
pub struct PostProperties {
pub height: f32,
/// Post height
pub is_new: bool,
/// This message is the focus of the view (formerly called is_main_event)
pub is_focused: bool,
/// This message is part of a thread
pub is_thread: bool,
/// Is this the first post in the display?
pub is_first: bool,
/// Is this the last post in the display
pub is_last: bool,
/// Position in the thread, focused message = 0
pub thread_position: i32,
}
macro_rules! theme_dispatch { macro_rules! theme_dispatch {
($($variant:path, $class:ident, $name:literal),+) => { ($($variant:path, $class:ident, $name:literal),+) => {
@ -143,55 +128,55 @@ macro_rules! theme_dispatch {
} }
} }
pub fn feed_post_separator_stroke(&self, post: &PostProperties) -> Stroke { pub fn feed_post_separator_stroke(&self, post: &NoteRenderData) -> Stroke {
match self.variant { match self.variant {
$( $variant => $class::feed_post_separator_stroke(self.dark_mode, post), )+ $( $variant => $class::feed_post_separator_stroke(self.dark_mode, post), )+
} }
} }
pub fn feed_post_outer_indent(&self, ui: &mut Ui, post: &PostProperties) { pub fn feed_post_outer_indent(&self, ui: &mut Ui, post: &NoteRenderData) {
match self.variant { match self.variant {
$( $variant => $class::feed_post_outer_indent(ui, post), )+ $( $variant => $class::feed_post_outer_indent(ui, post), )+
} }
} }
pub fn feed_post_inner_indent(&self, ui: &mut Ui, post: &PostProperties) { pub fn feed_post_inner_indent(&self, ui: &mut Ui, post: &NoteRenderData) {
match self.variant { match self.variant {
$( $variant => $class::feed_post_inner_indent(ui, post), )+ $( $variant => $class::feed_post_inner_indent(ui, post), )+
} }
} }
pub fn feed_frame_inner_margin(&self, post: &PostProperties) -> Margin { pub fn feed_frame_inner_margin(&self, post: &NoteRenderData) -> Margin {
match self.variant { match self.variant {
$( $variant => $class::feed_frame_inner_margin(post), )+ $( $variant => $class::feed_frame_inner_margin(post), )+
} }
} }
pub fn feed_frame_outer_margin(&self, post: &PostProperties) -> Margin { pub fn feed_frame_outer_margin(&self, post: &NoteRenderData) -> Margin {
match self.variant { match self.variant {
$( $variant => $class::feed_frame_outer_margin(post), )+ $( $variant => $class::feed_frame_outer_margin(post), )+
} }
} }
pub fn feed_frame_rounding(&self, post: &PostProperties) -> Rounding { pub fn feed_frame_rounding(&self, post: &NoteRenderData) -> Rounding {
match self.variant { match self.variant {
$( $variant => $class::feed_frame_rounding(post), )+ $( $variant => $class::feed_frame_rounding(post), )+
} }
} }
pub fn feed_frame_shadow(&self, post: &PostProperties) -> Shadow { pub fn feed_frame_shadow(&self, post: &NoteRenderData) -> Shadow {
match self.variant { match self.variant {
$( $variant => $class::feed_frame_shadow(self.dark_mode, post), )+ $( $variant => $class::feed_frame_shadow(self.dark_mode, post), )+
} }
} }
pub fn feed_frame_fill(&self, post: &PostProperties) -> Color32 { pub fn feed_frame_fill(&self, post: &NoteRenderData) -> Color32 {
match self.variant { match self.variant {
$( $variant => $class::feed_frame_fill(self.dark_mode, post), )+ $( $variant => $class::feed_frame_fill(self.dark_mode, post), )+
} }
} }
pub fn feed_frame_stroke(&self, post: &PostProperties) -> Stroke { pub fn feed_frame_stroke(&self, post: &NoteRenderData) -> Stroke {
match self.variant { match self.variant {
$( $variant => $class::feed_frame_stroke(self.dark_mode, post), )+ $( $variant => $class::feed_frame_stroke(self.dark_mode, post), )+
} }
@ -241,15 +226,15 @@ pub trait ThemeDef: Send + Sync {
fn feed_scroll_rounding(feed: &FeedProperties) -> Rounding; fn feed_scroll_rounding(feed: &FeedProperties) -> Rounding;
fn feed_scroll_fill(dark_mode: bool, feed: &FeedProperties) -> Color32; fn feed_scroll_fill(dark_mode: bool, feed: &FeedProperties) -> Color32;
fn feed_scroll_stroke(dark_mode: bool, feed: &FeedProperties) -> Stroke; fn feed_scroll_stroke(dark_mode: bool, feed: &FeedProperties) -> Stroke;
fn feed_post_separator_stroke(dark_mode: bool, post: &PostProperties) -> Stroke; fn feed_post_separator_stroke(dark_mode: bool, post: &NoteRenderData) -> Stroke;
fn feed_post_outer_indent(ui: &mut Ui, post: &PostProperties); fn feed_post_outer_indent(ui: &mut Ui, post: &NoteRenderData);
fn feed_post_inner_indent(ui: &mut Ui, post: &PostProperties); fn feed_post_inner_indent(ui: &mut Ui, post: &NoteRenderData);
fn feed_frame_inner_margin(post: &PostProperties) -> Margin; fn feed_frame_inner_margin(post: &NoteRenderData) -> Margin;
fn feed_frame_outer_margin(post: &PostProperties) -> Margin; fn feed_frame_outer_margin(post: &NoteRenderData) -> Margin;
fn feed_frame_rounding(post: &PostProperties) -> Rounding; fn feed_frame_rounding(post: &NoteRenderData) -> Rounding;
fn feed_frame_shadow(dark_mode: bool, post: &PostProperties) -> Shadow; fn feed_frame_shadow(dark_mode: bool, post: &NoteRenderData) -> Shadow;
fn feed_frame_fill(dark_mode: bool, post: &PostProperties) -> Color32; fn feed_frame_fill(dark_mode: bool, post: &NoteRenderData) -> Color32;
fn feed_frame_stroke(dark_mode: bool, post: &PostProperties) -> Stroke; fn feed_frame_stroke(dark_mode: bool, post: &NoteRenderData) -> Stroke;
// image rounding // image rounding
fn round_image() -> bool; fn round_image() -> bool;

View File

@ -1,4 +1,4 @@
use super::{FeedProperties, PostProperties, ThemeDef}; use super::{FeedProperties, NoteRenderData, ThemeDef};
use crate::ui::HighlightType; use crate::ui::HighlightType;
use eframe::egui::style::{Selection, WidgetVisuals, Widgets}; use eframe::egui::style::{Selection, WidgetVisuals, Widgets};
use eframe::egui::{FontDefinitions, Margin, Style, TextFormat, TextStyle, Visuals}; use eframe::egui::{FontDefinitions, Margin, Style, TextFormat, TextStyle, Visuals};
@ -380,17 +380,17 @@ impl ThemeDef for RoundyTheme {
fn feed_scroll_stroke(_dark_mode: bool, _feed: &FeedProperties) -> Stroke { fn feed_scroll_stroke(_dark_mode: bool, _feed: &FeedProperties) -> Stroke {
Stroke::NONE Stroke::NONE
} }
fn feed_post_separator_stroke(_dark_mode: bool, _post: &PostProperties) -> Stroke { fn feed_post_separator_stroke(_dark_mode: bool, _post: &NoteRenderData) -> Stroke {
Stroke::new(1.0, Color32::TRANSPARENT) Stroke::new(1.0, Color32::TRANSPARENT)
} }
fn feed_post_outer_indent(ui: &mut eframe::egui::Ui, post: &PostProperties) { fn feed_post_outer_indent(ui: &mut eframe::egui::Ui, post: &NoteRenderData) {
if post.is_thread { if post.is_thread {
let space = 100.0 * (10.0 - (1000.0 / (post.thread_position as f32 + 100.0))); let space = 100.0 * (10.0 - (1000.0 / (post.thread_position as f32 + 100.0)));
ui.add_space(space); ui.add_space(space);
} }
} }
fn feed_post_inner_indent(_ui: &mut eframe::egui::Ui, _post: &PostProperties) {} fn feed_post_inner_indent(_ui: &mut eframe::egui::Ui, _post: &NoteRenderData) {}
fn feed_frame_inner_margin(_post: &PostProperties) -> Margin { fn feed_frame_inner_margin(_post: &NoteRenderData) -> Margin {
Margin { Margin {
left: 10.0, left: 10.0,
right: 10.0, right: 10.0,
@ -398,10 +398,10 @@ impl ThemeDef for RoundyTheme {
bottom: 5.0, bottom: 5.0,
} }
} }
fn feed_frame_outer_margin(_post: &PostProperties) -> Margin { fn feed_frame_outer_margin(_post: &NoteRenderData) -> Margin {
Margin::default() Margin::default()
} }
fn feed_frame_rounding(post: &PostProperties) -> Rounding { fn feed_frame_rounding(post: &NoteRenderData) -> Rounding {
if post.is_thread { if post.is_thread {
let mut rounding = Rounding::none(); let mut rounding = Rounding::none();
if post.is_first && post.thread_position == 0 { if post.is_first && post.thread_position == 0 {
@ -413,10 +413,10 @@ impl ThemeDef for RoundyTheme {
Rounding::same(7.0) Rounding::same(7.0)
} }
} }
fn feed_frame_shadow(_dark_mode: bool, _post: &PostProperties) -> Shadow { fn feed_frame_shadow(_dark_mode: bool, _post: &NoteRenderData) -> Shadow {
Shadow::NONE Shadow::NONE
} }
fn feed_frame_fill(dark_mode: bool, post: &PostProperties) -> Color32 { fn feed_frame_fill(dark_mode: bool, post: &NoteRenderData) -> Color32 {
if post.is_new { if post.is_new {
if dark_mode { if dark_mode {
Color32::from_rgb(45, 45, 46) Color32::from_rgb(45, 45, 46)
@ -431,7 +431,7 @@ impl ThemeDef for RoundyTheme {
} }
} }
} }
fn feed_frame_stroke(dark_mode: bool, post: &PostProperties) -> Stroke { fn feed_frame_stroke(dark_mode: bool, post: &NoteRenderData) -> Stroke {
if post.is_focused { if post.is_focused {
if dark_mode { if dark_mode {
Stroke::new(1.0, Color32::from_rgb(64, 96, 64)) Stroke::new(1.0, Color32::from_rgb(64, 96, 64))