Skip to content

Commit

Permalink
feat: rtl toolbar item setting
Browse files Browse the repository at this point in the history
  • Loading branch information
zoli committed Nov 17, 2023
1 parent 8179419 commit 1e15a91
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage> {
LayoutDirection.rtlLayout;
final textDirection = isRTL ? TextDirection.rtl : TextDirection.ltr;

_setRTLToolbarItems(isRTL);
_setRTLToolbarItems(
context.read<AppearanceSettingsCubit>().state.enableRtlToolbarItems,
);

final editorScrollController = EditorScrollController(
editorState: widget.editorState,
Expand Down Expand Up @@ -406,14 +408,14 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage> {
);
}

void _setRTLToolbarItems(bool isRTL) {
void _setRTLToolbarItems(bool enableRtlToolbarItems) {
final textDirectionItemIds = textDirectionItems.map((e) => e.id);
// clear all the text direction items
toolbarItems.removeWhere(
(item) => textDirectionItemIds.contains(item.id),
);
// only show the rtl item when the layout direction is ltr.
if (isRTL) {
if (enableRtlToolbarItems) {
toolbarItems.addAll(textDirectionItems);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class AppearanceSettingsCubit extends Cubit<AppearanceSettingsState> {
appearanceSettings.monospaceFont,
appearanceSettings.layoutDirection,
appearanceSettings.textDirection,
appearanceSettings.enableRtlToolbarItems,
appearanceSettings.locale,
appearanceSettings.isMenuCollapsed,
appearanceSettings.menuOffset,
Expand Down Expand Up @@ -100,6 +101,12 @@ class AppearanceSettingsCubit extends Cubit<AppearanceSettingsState> {
emit(state.copyWith(textDirection: textDirection));
}

void setEnableRTLToolbarItems(bool value) {
_appearanceSettings.enableRtlToolbarItems = value;
_saveAppearanceSettings();
emit(state.copyWith(enableRtlToolbarItems: value));
}

/// Update selected font in the user's settings and emit an updated state
/// with the font name.
void setFontFamily(String fontFamilyName) {
Expand Down Expand Up @@ -307,6 +314,7 @@ class AppearanceSettingsState with _$AppearanceSettingsState {
required String monospaceFont,
required LayoutDirection layoutDirection,
required AppFlowyTextDirection? textDirection,
required bool enableRtlToolbarItems,
required Locale locale,
required bool isMenuCollapsed,
required double menuOffset,
Expand All @@ -322,6 +330,7 @@ class AppearanceSettingsState with _$AppearanceSettingsState {
String monospaceFont,
LayoutDirectionPB layoutDirectionPB,
TextDirectionPB? textDirectionPB,
bool enableRtlToolbarItems,
LocaleSettingsPB localePB,
bool isMenuCollapsed,
double menuOffset,
Expand All @@ -335,6 +344,7 @@ class AppearanceSettingsState with _$AppearanceSettingsState {
monospaceFont: monospaceFont,
layoutDirection: LayoutDirection.fromLayoutDirectionPB(layoutDirectionPB),
textDirection: AppFlowyTextDirection.fromTextDirectionPB(textDirectionPB),
enableRtlToolbarItems: enableRtlToolbarItems,
themeMode: _themeModeFromPB(themeModePB),
locale: Locale(localePB.languageCode, localePB.countryCode),
isMenuCollapsed: isMenuCollapsed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,28 @@ class TextDirectionSetting extends StatelessWidget {
}
}
}

class EnableRTLToolbarItemsSetting extends StatelessWidget {
const EnableRTLToolbarItemsSetting({
super.key,
});

@override
Widget build(BuildContext context) {
return ThemeSettingEntryTemplateWidget(
label: LocaleKeys.settings_appearance_enableRTLToolbarItems.tr(),
trailing: [
Switch(
value: context.read<AppearanceSettingsCubit>().state.enableRtlToolbarItems,
splashRadius: 0,
activeColor: Theme.of(context).colorScheme.primary,
onChanged: (value) {
context
.read<AppearanceSettingsCubit>()
.setEnableRTLToolbarItems(value);
},
),
],
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class SettingsAppearanceView extends StatelessWidget {
TextDirectionSetting(
currentTextDirection: state.textDirection,
),
EnableRTLToolbarItemsSetting(),
const Divider(),
DateFormatSetting(
currentFormat: state.dateFormat,
Expand Down
3 changes: 2 additions & 1 deletion frontend/resources/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@
"twelveHour": "Twelve hour",
"twentyFourHour": "Twenty four hour"
},
"showNamingDialogWhenCreatingPage": "Show naming dialog when creating a page"
"showNamingDialogWhenCreatingPage": "Show naming dialog when creating a page",
"enableRTLToolbarItems": "Enable RTL toolbar items"
},
"files": {
"copy": "Copy",
Expand Down
6 changes: 6 additions & 0 deletions frontend/rust-lib/flowy-user/src/entities/user_setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ pub struct AppearanceSettingsPB {
#[pb(index = 11)]
#[serde(default)]
pub text_direction: TextDirectionPB,

#[pb(index = 12)]
#[serde(default)]
pub enable_rtl_toolbar_items: bool,
}

const DEFAULT_RESET_VALUE: fn() -> bool = || APPEARANCE_RESET_AS_DEFAULT;
Expand Down Expand Up @@ -116,6 +120,7 @@ pub const APPEARANCE_DEFAULT_MONOSPACE_FONT: &str = "SF Mono";
const APPEARANCE_RESET_AS_DEFAULT: bool = true;
const APPEARANCE_DEFAULT_IS_MENU_COLLAPSED: bool = false;
const APPEARANCE_DEFAULT_MENU_OFFSET: f64 = 0.0;
const APPEARANCE_DEFAULT_ENABLE_RTL_TOOLBAR_ITEMS: bool = false;

impl std::default::Default for AppearanceSettingsPB {
fn default() -> Self {
Expand All @@ -131,6 +136,7 @@ impl std::default::Default for AppearanceSettingsPB {
menu_offset: APPEARANCE_DEFAULT_MENU_OFFSET,
layout_direction: LayoutDirectionPB::default(),
text_direction: TextDirectionPB::default(),
enable_rtl_toolbar_items: APPEARANCE_DEFAULT_ENABLE_RTL_TOOLBAR_ITEMS,
}
}
}
Expand Down

0 comments on commit 1e15a91

Please sign in to comment.