Skip to content

Commit

Permalink
mobile: make mobile flag runtime-configurable
Browse files Browse the repository at this point in the history
we need to pass a few more things around but it's not that bad. This
will allow you to launch damus with --mobile for mobile testing without
recompilation.
  • Loading branch information
jb55 committed May 31, 2024
1 parent 83eab71 commit 2305f1e
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 99 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ security-framework = "2.11.0"

[features]
default = []
emulate_mobile = []
profiling = ["puffin", "puffin_egui", "eframe/puffin"]

[profile.small]
Expand Down
20 changes: 2 additions & 18 deletions preview
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
#!/usr/bin/env bash

MOBILE_FLAG=0
FILTERED_ARGS=()

# Loop through the command line arguments
for arg in "$@"; do
if [[ "$arg" == "--mobile" ]]; then
MOBILE_FLAG=1
else
# Add non '--mobile' arguments to the filtered list
FILTERED_ARGS+=("$arg")
fi
done

if [[ "$MOBILE_FLAG" -eq 1 ]]; then
cargo run --bin ui_preview --features emulate_mobile --release -- "${FILTERED_ARGS[@]}"
else
cargo run --bin ui_preview --release -- "$@"
fi
# pass --mobile for mobile previews
cargo run --bin ui_preview --release -- "$@"
50 changes: 26 additions & 24 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use crate::notecache::{CachedNote, NoteCache};
use crate::route::Route;
use crate::timeline;
use crate::timeline::{NoteRef, Timeline, ViewFilter};
use crate::ui;
use crate::ui::profile::SimpleProfilePreviewController;
use crate::ui::{is_mobile, DesktopSidePanel};
use crate::ui::DesktopSidePanel;
use crate::Result;

use egui::{Context, Frame, Style};
Expand Down Expand Up @@ -38,6 +39,7 @@ pub struct Damus {
//compose: String,
note_cache: NoteCache,
pool: RelayPool,
is_mobile: bool,

/// global navigation for account management popups, etc.
nav: Vec<Route>,
Expand Down Expand Up @@ -599,7 +601,7 @@ fn process_message(damus: &mut Damus, relay: &str, msg: &RelayMessage) {
}

fn render_damus(damus: &mut Damus, ctx: &Context) {
if is_mobile() {
if damus.is_mobile() {
render_damus_mobile(ctx, damus);
} else {
render_damus_desktop(ctx, damus);
Expand All @@ -618,36 +620,32 @@ impl Damus {
data_path: P,
args: Vec<String>,
) -> Self {
// This is also where you can customized the look at feel of egui using
// `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`.

// Load previous app state (if any).
// Note that you must enable the `persistence` feature for this to work.
//if let Some(storage) = cc.storage {
//return eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();
//}
//

setup_cc(cc);

let mut timelines: Vec<Timeline> = vec![];
let _initial_limit = 100;
let mut is_mobile: Option<bool> = None;
if args.len() > 1 {
for arg in &args[1..] {
let filter = serde_json::from_str(arg).unwrap();
timelines.push(Timeline::new(filter));
if arg == "--mobile" {
is_mobile = Some(true);
} else if let Ok(filter) = serde_json::from_str(arg) {
timelines.push(Timeline::new(filter));
}
}
} else {
let filter = serde_json::from_str(include_str!("../queries/timeline.json")).unwrap();
timelines.push(Timeline::new(filter));
};

let is_mobile = is_mobile.unwrap_or(ui::is_compiled_as_mobile());

setup_cc(cc, is_mobile);

let imgcache_dir = data_path.as_ref().join(ImageCache::rel_datadir());
let _ = std::fs::create_dir_all(imgcache_dir.clone());

let mut config = Config::new();
config.set_ingester_threads(2);
Self {
is_mobile,
state: DamusState::Initializing,
pool: RelayPool::new(),
img_cache: ImageCache::new(imgcache_dir),
Expand All @@ -668,9 +666,8 @@ impl Damus {
}
}

pub fn mock<P: AsRef<Path>>(data_path: P) -> Self {
pub fn mock<P: AsRef<Path>>(data_path: P, is_mobile: bool) -> Self {
let mut timelines: Vec<Timeline> = vec![];
let _initial_limit = 100;
let filter = serde_json::from_str(include_str!("../queries/global.json")).unwrap();
timelines.push(Timeline::new(filter));

Expand All @@ -680,6 +677,7 @@ impl Damus {
let mut config = Config::new();
config.set_ingester_threads(2);
Self {
is_mobile,
state: DamusState::Initializing,
pool: RelayPool::new(),
img_cache: ImageCache::new(imgcache_dir),
Expand Down Expand Up @@ -727,6 +725,10 @@ impl Damus {
}
self.selected_timeline += 1;
}

pub fn is_mobile(&self) -> bool {
self.is_mobile
}
}

/*
Expand Down Expand Up @@ -763,7 +765,7 @@ fn render_panel(ctx: &egui::Context, app: &mut Damus, timeline_ind: usize) {
ui.visuals_mut().button_frame = false;

if let Some(new_visuals) =
user_requested_visuals_change(is_mobile(), ctx.style().visuals.dark_mode, ui)
user_requested_visuals_change(app.is_mobile(), ctx.style().visuals.dark_mode, ui)
{
ctx.set_visuals(new_visuals)
}
Expand Down Expand Up @@ -820,14 +822,14 @@ fn render_damus_mobile(ctx: &egui::Context, app: &mut Damus) {
#[cfg(feature = "profiling")]
puffin::profile_function!();

main_panel(&ctx.style()).show(ctx, |ui| {
main_panel(&ctx.style(), app.is_mobile()).show(ctx, |ui| {
timeline::timeline_view(ui, app, 0);
});
}

fn main_panel(style: &Style) -> egui::CentralPanel {
fn main_panel(style: &Style, mobile: bool) -> egui::CentralPanel {
let inner_margin = egui::Margin {
top: if crate::ui::is_mobile() { 50.0 } else { 0.0 },
top: if mobile { 50.0 } else { 0.0 },
left: 0.0,
right: 0.0,
bottom: 0.0,
Expand All @@ -854,7 +856,7 @@ fn render_damus_desktop(ctx: &egui::Context, app: &mut Damus) {
Size::remainder()
};

main_panel(&ctx.style()).show(ctx, |ui| {
main_panel(&ctx.style(), app.is_mobile()).show(ctx, |ui| {
ui.spacing_mut().item_spacing.x = 0.0;
if need_scroll {
egui::ScrollArea::horizontal().show(ui, |ui| {
Expand Down
7 changes: 3 additions & 4 deletions src/app_creation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::app_style::{create_custom_style, dark_mode, desktop_font_size, mobile_font_size};
use crate::fonts::setup_fonts;
use crate::ui::is_mobile;
use eframe::NativeOptions;

//pub const UI_SCALE_FACTOR: f32 = 0.2;
Expand Down Expand Up @@ -36,7 +35,7 @@ pub fn generate_mobile_emulator_native_options() -> eframe::NativeOptions {
})
}

pub fn setup_cc(cc: &eframe::CreationContext<'_>) {
pub fn setup_cc(cc: &eframe::CreationContext<'_>, is_mobile: bool) {
let ctx = &cc.egui_ctx;
setup_fonts(ctx);

Expand All @@ -48,9 +47,9 @@ pub fn setup_cc(cc: &eframe::CreationContext<'_>) {

egui_extras::install_image_loaders(ctx);

ctx.set_visuals(dark_mode(is_mobile()));
ctx.set_visuals(dark_mode(is_mobile));

ctx.set_style(if is_mobile() {
ctx.set_style(if is_mobile {
create_custom_style(ctx, mobile_font_size)
} else {
create_custom_style(ctx, desktop_font_size)
Expand Down
20 changes: 11 additions & 9 deletions src/ui/account_login_view.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
use crate::app_style::NotedeckTextStyle;
use crate::key_parsing::LoginError;
use crate::login_manager::LoginManager;
use crate::ui;
use crate::ui::{Preview, View};
use crate::ui::{Preview, PreviewConfig, View};
use egui::{
Align, Align2, Button, Color32, Frame, Id, LayerId, Margin, Pos2, Rect, RichText, Rounding, Ui,
Vec2, Window,
};
use egui::{Image, TextEdit};

pub struct AccountLoginView<'a> {
is_mobile: bool,
manager: &'a mut LoginManager,
generate_y_intercept: Option<f32>,
}

impl<'a> View for AccountLoginView<'a> {
fn ui(&mut self, ui: &mut egui::Ui) {
let is_mobile = ui::is_mobile();
if let Some(key) = self.manager.check_for_successful_login() {
// TODO: route to "home"
println!("successful login with key: {:?}", key);
/*
return if is_mobile {
return if self.mobile {
// route to "home" on mobile
} else {
// route to "home" on desktop
};
*/
}
if is_mobile {
if self.is_mobile {
self.show_mobile(ui);
} else {
self.show(ui);
Expand All @@ -37,8 +36,9 @@ impl<'a> View for AccountLoginView<'a> {
}

impl<'a> AccountLoginView<'a> {
pub fn new(manager: &'a mut LoginManager) -> Self {
pub fn new(manager: &'a mut LoginManager, is_mobile: bool) -> Self {
AccountLoginView {
is_mobile,
manager,
generate_y_intercept: None,
}
Expand Down Expand Up @@ -361,20 +361,22 @@ fn login_textedit(manager: &mut LoginManager) -> TextEdit {
}

pub struct AccountLoginPreview {
is_mobile: bool,
manager: LoginManager,
}

impl View for AccountLoginPreview {
fn ui(&mut self, ui: &mut egui::Ui) {
AccountLoginView::new(&mut self.manager).ui(ui);
AccountLoginView::new(&mut self.manager, self.is_mobile).ui(ui);
}
}

impl<'a> Preview for AccountLoginView<'a> {
type Prev = AccountLoginPreview;

fn preview() -> Self::Prev {
fn preview(cfg: PreviewConfig) -> Self::Prev {
let manager = LoginManager::new();
AccountLoginPreview { manager }
let is_mobile = cfg.is_mobile;
AccountLoginPreview { is_mobile, manager }
}
}
16 changes: 11 additions & 5 deletions src/ui/account_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ use crate::colors::PINK;
use crate::{
account_manager::AccountManager,
app_style::NotedeckTextStyle,
ui::{self, Preview, View},
ui::{Preview, PreviewConfig, View},
};
use egui::{Align, Button, Frame, Image, Layout, RichText, ScrollArea, Vec2};

use super::profile::preview::SimpleProfilePreview;
use super::profile::{ProfilePreviewOp, SimpleProfilePreviewController};

pub struct AccountManagementView<'a> {
mobile: bool,
account_manager: &'a mut AccountManager,
simple_preview_controller: SimpleProfilePreviewController<'a>,
}

impl<'a> View for AccountManagementView<'a> {
fn ui(&mut self, ui: &mut egui::Ui) {
if ui::is_mobile() {
if self.mobile {
self.show_mobile(ui);
} else {
self.show(ui);
Expand All @@ -26,10 +27,12 @@ impl<'a> View for AccountManagementView<'a> {

impl<'a> AccountManagementView<'a> {
pub fn new(
mobile: bool,
account_manager: &'a mut AccountManager,
simple_preview_controller: SimpleProfilePreviewController<'a>,
) -> Self {
AccountManagementView {
mobile,
account_manager,
simple_preview_controller,
}
Expand Down Expand Up @@ -236,16 +239,18 @@ mod preview {
use crate::{imgcache::ImageCache, test_data::get_accmgr_and_ndb_and_imgcache};

pub struct AccountManagementPreview {
is_mobile: bool,
account_manager: AccountManager,
ndb: Ndb,
img_cache: ImageCache,
}

impl AccountManagementPreview {
fn new() -> Self {
fn new(is_mobile: bool) -> Self {
let (account_manager, ndb, img_cache) = get_accmgr_and_ndb_and_imgcache();

AccountManagementPreview {
is_mobile,
account_manager,
ndb,
img_cache,
Expand All @@ -257,6 +262,7 @@ mod preview {
fn ui(&mut self, ui: &mut egui::Ui) {
ui.add_space(24.0);
AccountManagementView::new(
self.is_mobile,
&mut self.account_manager,
SimpleProfilePreviewController::new(&self.ndb, &mut self.img_cache),
)
Expand All @@ -267,8 +273,8 @@ mod preview {
impl<'a> Preview for AccountManagementView<'a> {
type Prev = AccountManagementPreview;

fn preview() -> Self::Prev {
AccountManagementPreview::new()
fn preview(cfg: PreviewConfig) -> Self::Prev {
AccountManagementPreview::new(cfg.is_mobile)
}
}
}
Loading

0 comments on commit 2305f1e

Please sign in to comment.