mobile: make mobile flag runtime-configurable

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.
This commit is contained in:
William Casarin
2024-05-31 00:52:27 -05:00
parent 83eab71148
commit 2305f1e50a
15 changed files with 101 additions and 99 deletions

View File

@@ -1,8 +1,7 @@
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,
@@ -10,25 +9,25 @@ use egui::{
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);
@@ -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,
}
@@ -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 }
}
}

View File

@@ -2,7 +2,7 @@ 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};
@@ -10,13 +10,14 @@ 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);
@@ -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,
}
@@ -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,
@@ -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),
)
@@ -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)
}
}
}

View File

@@ -2,7 +2,7 @@ use crate::{
account_manager::{AccountManager, UserAccount},
colors::PINK,
profile::DisplayName,
ui, Result,
Result,
};
use egui::{
Align, Button, Color32, Frame, Id, Image, Layout, Margin, RichText, Rounding, ScrollArea,
@@ -12,6 +12,7 @@ use egui::{
use super::profile::{preview::SimpleProfilePreview, SimpleProfilePreviewController};
pub struct AccountSelectionWidget<'a> {
is_mobile: bool,
account_manager: &'a AccountManager,
simple_preview_controller: SimpleProfilePreviewController<'a>,
}
@@ -29,17 +30,19 @@ struct AccountSelectResponse {
impl<'a> AccountSelectionWidget<'a> {
pub fn new(
is_mobile: bool,
account_manager: &'a AccountManager,
simple_preview_controller: SimpleProfilePreviewController<'a>,
) -> Self {
AccountSelectionWidget {
is_mobile,
account_manager,
simple_preview_controller,
}
}
pub fn ui(&'a mut self, ui: &mut egui::Ui) {
if ui::is_mobile() {
if self.is_mobile {
self.show_mobile(ui);
} else {
self.show(ui);
@@ -218,21 +221,23 @@ mod previews {
account_manager::AccountManager,
imgcache::ImageCache,
test_data,
ui::{profile::SimpleProfilePreviewController, Preview, View},
ui::{profile::SimpleProfilePreviewController, Preview, PreviewConfig, View},
};
use super::AccountSelectionWidget;
pub struct AccountSelectionPreview {
is_mobile: bool,
account_manager: AccountManager,
ndb: Ndb,
img_cache: ImageCache,
}
impl AccountSelectionPreview {
fn new() -> Self {
fn new(is_mobile: bool) -> Self {
let (account_manager, ndb, img_cache) = test_data::get_accmgr_and_ndb_and_imgcache();
AccountSelectionPreview {
is_mobile,
account_manager,
ndb,
img_cache,
@@ -243,6 +248,7 @@ mod previews {
impl View for AccountSelectionPreview {
fn ui(&mut self, ui: &mut egui::Ui) {
AccountSelectionWidget::new(
self.is_mobile,
&self.account_manager,
SimpleProfilePreviewController::new(&self.ndb, &mut self.img_cache),
)
@@ -253,8 +259,8 @@ mod previews {
impl<'a> Preview for AccountSelectionWidget<'a> {
type Prev = AccountSelectionPreview;
fn preview() -> Self::Prev {
AccountSelectionPreview::new()
fn preview(cfg: PreviewConfig) -> Self::Prev {
AccountSelectionPreview::new(cfg.is_mobile)
}
}
}

View File

@@ -14,7 +14,7 @@ pub use account_management::AccountManagementView;
pub use account_switcher::AccountSelectionWidget;
pub use mention::Mention;
pub use note::Note;
pub use preview::{Preview, PreviewApp};
pub use preview::{Preview, PreviewApp, PreviewConfig};
pub use profile::{ProfilePic, ProfilePreview};
pub use relay::RelayView;
pub use side_panel::DesktopSidePanel;
@@ -53,16 +53,12 @@ pub fn hline(ui: &egui::Ui) {
#[inline]
#[allow(unreachable_code)]
pub fn is_mobile() -> bool {
#[cfg(feature = "emulate_mobile")]
{
return true;
}
pub fn is_compiled_as_mobile() -> bool {
#[cfg(any(target_os = "android", target_os = "ios"))]
{
true
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
false

View File

@@ -4,7 +4,7 @@ pub mod options;
pub use contents::NoteContents;
pub use options::NoteOptions;
use crate::{colors, notecache::CachedNote, ui, ui::is_mobile, Damus};
use crate::{colors, notecache::CachedNote, ui, Damus};
use egui::{Label, RichText, Sense};
use nostrdb::{NoteKey, Transaction};
use std::hash::{Hash, Hasher};
@@ -211,7 +211,7 @@ impl<'a> Note<'a> {
let profile_key = profile.as_ref().unwrap().record().note_key();
let note_key = note_key.as_u64();
if is_mobile() {
if self.app.is_mobile() {
ui.add(ui::ProfilePic::new(&mut self.app.img_cache, pic));
} else {
let (rect, size) = ui::anim::hover_expand(

View File

@@ -1,9 +1,13 @@
use crate::ui::View;
pub struct PreviewConfig {
pub is_mobile: bool,
}
pub trait Preview {
type Prev: View;
fn preview() -> Self::Prev;
fn preview(cfg: PreviewConfig) -> Self::Prev;
}
pub struct PreviewApp {

View File

@@ -1,5 +1,5 @@
use crate::imgcache::ImageCache;
use crate::ui::{Preview, View};
use crate::ui::{Preview, PreviewConfig, View};
use egui::{vec2, Sense, TextureHandle};
pub struct ProfilePic<'cache, 'url> {
@@ -185,7 +185,7 @@ mod preview {
impl<'cache, 'url> Preview for ProfilePic<'cache, 'url> {
type Prev = ProfilePicPreview;
fn preview() -> Self::Prev {
fn preview(_cfg: PreviewConfig) -> Self::Prev {
ProfilePicPreview::new()
}
}

View File

@@ -111,7 +111,7 @@ impl<'a, 'cache> egui::Widget for SimpleProfilePreview<'a, 'cache> {
mod previews {
use super::*;
use crate::test_data::test_profile_record;
use crate::ui::{Preview, View};
use crate::ui::{Preview, PreviewConfig, View};
pub struct ProfilePreviewPreview<'a> {
profile: ProfileRecord<'a>,
@@ -142,7 +142,7 @@ mod previews {
/// A preview of the profile preview :D
type Prev = ProfilePreviewPreview<'a>;
fn preview() -> Self::Prev {
fn preview(_cfg: PreviewConfig) -> Self::Prev {
ProfilePreviewPreview::new()
}
}

View File

@@ -1,5 +1,5 @@
use crate::relay_pool_manager::{RelayPoolManager, RelayStatus};
use crate::ui::{Preview, View};
use crate::ui::{Preview, PreviewConfig, View};
use egui::{Align, Button, Frame, Layout, Margin, Rgba, RichText, Rounding, Ui, Vec2};
use crate::app_style::NotedeckTextStyle;
@@ -203,7 +203,7 @@ mod preview {
impl<'a> Preview for RelayView<'a> {
type Prev = RelayViewPreview;
fn preview() -> Self::Prev {
fn preview(_cfg: PreviewConfig) -> Self::Prev {
RelayViewPreview::new()
}
}

View File

@@ -125,7 +125,11 @@ fn add_column_button(dark_mode: bool) -> egui::Button<'static> {
mod preview {
use nostrdb::Ndb;
use crate::{imgcache::ImageCache, test_data, ui::Preview};
use crate::{
imgcache::ImageCache,
test_data,
ui::{Preview, PreviewConfig},
};
use super::*;
@@ -165,7 +169,7 @@ mod preview {
impl<'a> Preview for DesktopSidePanel<'a> {
type Prev = DesktopSidePanelPreview;
fn preview() -> Self::Prev {
fn preview(_cfg: PreviewConfig) -> Self::Prev {
DesktopSidePanelPreview::new()
}
}