Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
surinder83singh committed Aug 23, 2024
1 parent 1782b14 commit ca95052
Show file tree
Hide file tree
Showing 11 changed files with 446 additions and 424 deletions.
648 changes: 293 additions & 355 deletions Cargo.lock

Large diffs are not rendered by default.

35 changes: 26 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ kaspa-ng-macros = { version = "0.2.3", path = "macros/" }

# egui = "=0.24.0"
# epaint = "=0.24.0"
# egui_plot = "=0.24.0"

# egui_extras = { version = "=0.24.0", features = ["svg","image"] }
# eframe = { version = "=0.24.0", default-features = false, features = [
# "accesskit", # Make egui comptaible with screen readers. NOTE: adds a lot of dependencies.
Expand All @@ -39,19 +39,36 @@ kaspa-ng-macros = { version = "0.2.3", path = "macros/" }
# "persistence", # Enable restoring app state when restarting the app.
# ] }

egui = { path="../egui/crates/egui"}
epaint = { path="../egui/crates/epaint"}
egui_plot = { path="../egui/crates/egui_plot"}
egui_extras = { path="../egui/crates/egui_extras", features = ["svg","image", "serde"] }
eframe = { path="../egui/crates/eframe", default-features = false, features = [
# egui = { path="../egui/crates/egui", version="=0.28.1"}
# epaint = { path="../egui/crates/epaint"}
# # epaint = "=0.28.1"
# egui_plot = "=0.28.1"
# # egui_plot = { path="../egui/crates/egui_plot"}
# egui_extras = { path="../egui/crates/egui_extras", features = ["svg","image", "serde"] }
# eframe = { path="../egui/crates/eframe", default-features = false, features = [
# "accesskit", # Make egui comptaible with screen readers. NOTE: adds a lot of dependencies.
# "default_fonts", # Embed the default egui fonts.
# "glow", # Use the glow rendering backend. Alternative: "wgpu".
# "persistence", # Enable restoring app state when restarting the app.
# ] }
# egui-phosphor = { path = "../egui-phosphor", version = "=0.6.0", features = ["thin","light","regular","bold"] }
# egui-notify = { path = "../egui-notify" }
# egui-toast = { path = "../egui-toast" }

egui = { git = "https://github.com/aspectron/egui", branch = "omega"}
epaint = { git = "https://github.com/aspectron/egui", branch = "omega"}

egui_plot = { git = "https://github.com/aspectron/egui_plot", branch = "omega"}
egui_extras = { git = "https://github.com/aspectron/egui", branch = "omega", features = ["svg","image", "serde"] }
eframe = { git = "https://github.com/aspectron/egui", branch = "omega", default-features = false, features = [
"accesskit", # Make egui comptaible with screen readers. NOTE: adds a lot of dependencies.
"default_fonts", # Embed the default egui fonts.
"glow", # Use the glow rendering backend. Alternative: "wgpu".
"persistence", # Enable restoring app state when restarting the app.
] }
egui-phosphor = { version = "=0.3.1", features = ["thin","light","regular","bold"] }
egui-notify = "=0.11.0"
# egui-toast = "0.9.0"
egui-phosphor = { git = "https://github.com/aspectron/egui-phosphor", branch = "omega", version = "=0.6.0", features = ["thin","light","regular","bold"] }
egui-notify = { git = "https://github.com/aspectron/egui-notify", branch = "omega"}
# egui-toast = { git = "https://github.com/aspectron/egui-toast", branch = "omega" }

# egui = { path = "../egui/crates/egui" }
# egui_extras = { path = "../egui/crates/egui_extras", features = ["svg", "image"] }
Expand Down
7 changes: 4 additions & 3 deletions core/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ cfg_if! {
runtime::signals::Signals::bind(&runtime);
runtime.start();

Box::new(kaspa_ng_core::Core::new(cc, runtime, settings, window_frame))
Ok(Box::new(kaspa_ng_core::Core::new(cc, runtime, settings, window_frame)))
}),
)?;

Expand All @@ -302,6 +302,7 @@ cfg_if! {

// use crate::result::Result;
// use crate::adaptor::Adaptor;
use wasm_bindgen::JsCast;

// pub async fn kaspa_ng_main(wallet_api : Option<Arc<dyn WalletApi>>, application_events : Option<ApplicationEventsChannel>, adaptor: Option<Arc<Adaptor>>) -> Result<()> {
pub async fn kaspa_ng_main(application_context : ApplicationContext) -> Result<()> {
Expand Down Expand Up @@ -347,7 +348,7 @@ cfg_if! {

eframe::WebRunner::new()
.start(
"kaspa-ng",
document().get_element_by_id("kaspa-ng").expect("<canvas id=\"kaspa-ng\"> not found.").dyn_into::<web_sys::HtmlCanvasElement>().unwrap(),
web_options,
Box::new(move |cc| {

Expand All @@ -366,7 +367,7 @@ cfg_if! {



Box::new(kaspa_ng_core::Core::new(cc, runtime, settings, false))
Ok(Box::new(kaspa_ng_core::Core::new(cc, runtime, settings, false)))
}),
)
.await
Expand Down
49 changes: 41 additions & 8 deletions core/src/egui/composite_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,26 @@ impl Widget for CompositeButton<'_> {
secondary_text_style = TextStyle::Monospace;
}

let text = text.map(|text| text.into_galley(ui, wrap, text_wrap_width, TextStyle::Button));
let wrap_mode = wrap.map(|wrap| {
if wrap {
TextWrapMode::Wrap
} else {
TextWrapMode::Extend
}
});

let text =
text.map(|text| text.into_galley(ui, wrap_mode, text_wrap_width, TextStyle::Button));
let secondary_text = secondary_text
.map(|text| text.into_galley(ui, wrap, text_wrap_width, secondary_text_style));
let shortcut_text = (!shortcut_text.is_empty())
.then(|| shortcut_text.into_galley(ui, Some(false), f32::INFINITY, TextStyle::Button));
.map(|text| text.into_galley(ui, wrap_mode, text_wrap_width, secondary_text_style));
let shortcut_text = (!shortcut_text.is_empty()).then(|| {
shortcut_text.into_galley(
ui,
Some(TextWrapMode::Extend),
f32::INFINITY,
TextStyle::Button,
)
});

let mut desired_size = Vec2::new(pulldown_padding, 0.0); //Vec2::ZERO;
let mut img_plus_spacing_width = 0.0;
Expand Down Expand Up @@ -340,7 +355,7 @@ impl Widget for CompositeButton<'_> {
let (rect, mut response) = ui.allocate_at_least(desired_size, sense);
response.widget_info(|| {
if let Some(text) = &text {
WidgetInfo::labeled(WidgetType::Button, text.text())
WidgetInfo::labeled(WidgetType::Button, true, text.text())
} else {
WidgetInfo::new(WidgetType::Button)
}
Expand Down Expand Up @@ -397,11 +412,23 @@ impl Widget for CompositeButton<'_> {
image.image_options(),
);

response = texture_load_result_response(image.source(), &tlr, response);
response =
texture_load_result_response(&image.source(ui.ctx()), &tlr, response);
}
Composite::Icon(icon) => {
let galley = WidgetText::RichText(icon.clone().size(image_size.y))
.into_galley(ui, wrap, text_wrap_width, TextStyle::Button);
.into_galley(
ui,
wrap.map(|wrap| {
if wrap {
TextWrapMode::Wrap
} else {
TextWrapMode::Extend
}
}),
text_wrap_width,
TextStyle::Button,
);
let image_rect = Rect::from_min_size(
pos2(cursor_x, rect.center().y - 0.5 - (galley.size().y / 2.0)),
galley.size(),
Expand Down Expand Up @@ -472,7 +499,13 @@ impl Widget for CompositeButton<'_> {
if pulldown_selector {
let galley = WidgetText::RichText(RichText::new("⏷").size(14.)).into_galley(
ui,
wrap,
wrap.map(|wrap| {
if wrap {
TextWrapMode::Wrap
} else {
TextWrapMode::Extend
}
}),
text_wrap_width,
TextStyle::Button,
);
Expand Down
81 changes: 57 additions & 24 deletions core/src/egui/composite_icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@
use egui::*;
use std::sync::Arc;

pub struct LayoutResult {
pos: Pos2,
text_pos: Pos2,
response: Response,
icon_text: Arc<Galley>,
text: Option<Arc<Galley>>,
secondary_text: Option<Arc<Galley>>,
}
impl LayoutResult {
fn new(
pos: Pos2,
text_pos: Pos2,
response: Response,
icon_text: Arc<Galley>,
text: Option<Arc<Galley>>,
secondary_text: Option<Arc<Galley>>,
) -> Self {
Self {
pos,
text_pos,
response,
icon_text,
text,
secondary_text,
}
}
}

/// Clickable button with text.
///
/// See also [`Ui::button`].
Expand Down Expand Up @@ -156,17 +184,7 @@ impl CompositeIcon {
}

/// Do layout and position the galley in the ui, without painting it or adding widget info.
pub fn layout_in_ui(
&self,
ui: &mut Ui,
) -> (
Pos2,
Pos2,
Response,
Arc<Galley>,
Option<Arc<Galley>>,
Option<Arc<Galley>>,
) {
pub fn layout_in_ui(&self, ui: &mut Ui) -> LayoutResult {
let sense = {
// We only want to focus icon if the screen reader is on.
if ui.memory(|mem| mem.options.screen_reader) {
Expand All @@ -185,14 +203,22 @@ impl CompositeIcon {
secondary_text_style = TextStyle::Body;
}

let text = self
.text
.clone()
.map(|text| text.into_galley(ui, Some(true), text_wrap_width, TextStyle::Button));
let secondary_text = self
.secondary_text
.clone()
.map(|text| text.into_galley(ui, Some(true), text_wrap_width, secondary_text_style));
let text = self.text.clone().map(|text| {
text.into_galley(
ui,
Some(TextWrapMode::Wrap),
text_wrap_width,
TextStyle::Button,
)
});
let secondary_text = self.secondary_text.clone().map(|text| {
text.into_galley(
ui,
Some(TextWrapMode::Wrap),
text_wrap_width,
secondary_text_style,
)
});

if let Some(text) = &text {
text_size += text.size();
Expand Down Expand Up @@ -222,7 +248,7 @@ impl CompositeIcon {

// h-center
//pos.x = response.rect.left() + (response.rect.width() - icon_size.x)/2.0;
(pos, text_pos, response, text_galley, text, secondary_text)
LayoutResult::new(pos, text_pos, response, text_galley, text, secondary_text)
};

// if let WidgetText::Galley(galley) = self.icon.clone() {
Expand Down Expand Up @@ -250,7 +276,7 @@ impl CompositeIcon {
.into_layout_job(ui.style(), FontSelection::Default, valign);

let truncate = true;
let wrap = !truncate && ui.wrap_text();
let wrap = !truncate && ui.wrap_mode() == TextWrapMode::Wrap;
let available_width = ui.available_width();

if wrap
Expand All @@ -263,7 +289,7 @@ impl CompositeIcon {

let cursor = ui.cursor();
let first_row_indentation = available_width - ui.available_size_before_wrap().x;
egui_assert!(first_row_indentation.is_finite());
assert!(first_row_indentation.is_finite());

layout_job.wrap.max_width = available_width;
layout_job.first_row_min_height = cursor.height();
Expand Down Expand Up @@ -328,11 +354,18 @@ impl CompositeIcon {

impl Widget for CompositeIcon {
fn ui(self, ui: &mut Ui) -> Response {
let (pos, text_pos, response, icon_text, text, secondary_text) = self.layout_in_ui(ui);
let LayoutResult {
pos,
text_pos,
response,
icon_text,
text,
secondary_text,
} = self.layout_in_ui(ui);

response.widget_info(|| {
if let Some(text) = &self.text {
WidgetInfo::labeled(WidgetType::Button, text.text())
WidgetInfo::labeled(WidgetType::Button, true, text.text())
} else {
WidgetInfo::new(WidgetType::Button)
}
Expand Down
26 changes: 13 additions & 13 deletions core/src/egui/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ pub use style::*;
use crate::imports::*;

#[derive(Clone)]
pub struct Theme {
pub struct AppTheme {
pub color: ThemeColor,
pub style: ThemeStyle,
}

impl Theme {
impl AppTheme {
pub fn new(color: ThemeColor, style: ThemeStyle) -> Self {
Self { color, style }
}
Expand All @@ -29,7 +29,7 @@ impl Theme {
}
}

impl Default for Theme {
impl Default for AppTheme {
fn default() -> Self {
Self {
color: ThemeColor::dark(),
Expand All @@ -38,8 +38,8 @@ impl Default for Theme {
}
}

impl From<&Theme> for Visuals {
fn from(theme: &Theme) -> Self {
impl From<&AppTheme> for Visuals {
fn from(theme: &AppTheme) -> Self {
let mut visuals = if theme.color.dark_mode {
Visuals::dark()
} else {
Expand All @@ -62,16 +62,16 @@ impl From<&Theme> for Visuals {
}
}

impl AsRef<Theme> for Theme {
impl AsRef<AppTheme> for AppTheme {
fn as_ref(&self) -> &Self {
self
}
}

static mut THEME: Option<Theme> = None;
static mut THEME: Option<AppTheme> = None;
#[inline(always)]
pub fn theme() -> &'static Theme {
unsafe { THEME.get_or_insert_with(Theme::default) }
pub fn theme() -> &'static AppTheme {
unsafe { THEME.get_or_insert_with(AppTheme::default) }
}

#[inline(always)]
Expand Down Expand Up @@ -107,7 +107,7 @@ pub fn apply_theme_by_name(
ThemeStyle::default()
});

apply_theme(ctx, Theme::new(theme_color, theme_style));
apply_theme(ctx, AppTheme::new(theme_color, theme_style));
}

pub fn apply_theme_color_by_name(ctx: &Context, theme_color_name: impl Into<String>) {
Expand All @@ -120,7 +120,7 @@ pub fn apply_theme_color_by_name(ctx: &Context, theme_color_name: impl Into<Stri
ThemeColor::default()
});

apply_theme(ctx, Theme::new(theme_color, theme_style().clone()));
apply_theme(ctx, AppTheme::new(theme_color, theme_style().clone()));
}

pub fn apply_theme_style_by_name(ctx: &Context, theme_style_name: impl Into<String>) {
Expand All @@ -133,10 +133,10 @@ pub fn apply_theme_style_by_name(ctx: &Context, theme_style_name: impl Into<Stri
ThemeStyle::default()
});

apply_theme(ctx, Theme::new(theme_color().clone(), theme_style));
apply_theme(ctx, AppTheme::new(theme_color().clone(), theme_style));
}

pub fn apply_theme(ctx: &Context, theme: Theme) {
pub fn apply_theme(ctx: &Context, theme: AppTheme) {
unsafe {
THEME = Some(theme.clone());
}
Expand Down
Loading

0 comments on commit ca95052

Please sign in to comment.