Browse Source

Calendar: Add setting to choose default view

This commit adds an entry to the Calendar Settings to allow the user to
select between the month and year views as the startup default.
Olivier De Cannière 2 years ago
parent
commit
a1d98b825d

+ 3 - 0
Userland/Applications/Calendar/main.cpp

@@ -99,6 +99,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
     view_type_action_group->set_exclusive(true);
     view_type_action_group->add_action(*view_month_action);
     view_type_action_group->add_action(*view_year_action);
+    auto default_view = Config::read_string("Calendar"sv, "View"sv, "DefaultView"sv, "Month"sv);
+    if (default_view == "Year")
+        view_year_action->set_checked(true);
 
     (void)TRY(toolbar->try_add_action(prev_date_action));
     (void)TRY(toolbar->try_add_action(next_date_action));

+ 11 - 0
Userland/Applications/CalendarSettings/CalendarSettingsWidget.cpp

@@ -13,16 +13,19 @@
 void CalendarSettingsWidget::apply_settings()
 {
     Config::write_string("Calendar"sv, "View"sv, "FirstDayOfWeek"sv, m_first_day_of_week_combobox->text());
+    Config::write_string("Calendar"sv, "View"sv, "DefaultView"sv, m_default_view_combobox->text());
 }
 
 void CalendarSettingsWidget::reset_default_values()
 {
     m_first_day_of_week_combobox->set_text("Sunday");
+    m_default_view_combobox->set_text("Month");
 }
 
 CalendarSettingsWidget::CalendarSettingsWidget()
 {
     load_from_gml(calendar_settings_widget_gml);
+
     m_first_day_of_week_combobox = *find_descendant_of_type_named<GUI::ComboBox>("first_day_of_week");
     m_first_day_of_week_combobox->set_text(Config::read_string("Calendar"sv, "View"sv, "FirstDayOfWeek"sv, "Sunday"sv));
     m_first_day_of_week_combobox->set_only_allow_values_from_model(true);
@@ -30,4 +33,12 @@ CalendarSettingsWidget::CalendarSettingsWidget()
     m_first_day_of_week_combobox->on_change = [&](auto, auto) {
         set_modified(true);
     };
+
+    m_default_view_combobox = *find_descendant_of_type_named<GUI::ComboBox>("default_view");
+    m_default_view_combobox->set_text(Config::read_string("Calendar"sv, "View"sv, "DefaultView"sv, "Month"sv));
+    m_default_view_combobox->set_only_allow_values_from_model(true);
+    m_default_view_combobox->set_model(*GUI::ItemListModel<StringView, Array<StringView, 2>>::create(m_view_modes));
+    m_default_view_combobox->on_change = [&](auto, auto) {
+        set_modified(true);
+    };
 }

+ 34 - 3
Userland/Applications/CalendarSettings/CalendarSettingsWidget.gml

@@ -30,9 +30,40 @@
                 fixed_width: 70
             }
 
-           @GUI::ComboBox {
-               name: "first_day_of_week"
-           }
+            @GUI::ComboBox {
+                name: "first_day_of_week"
+            }
+        }
+    }
+
+    @GUI::GroupBox {
+        title: "Default view"
+        fixed_height: 72
+        layout: @GUI::VerticalBoxLayout {
+            margins: [6]
+            spacing: 2
+        }
+
+        @GUI::Label {
+            text: "Show the month or the year view when Calendar is started."
+            word_wrap: true
+            text_alignment: "CenterLeft"
+        }
+
+        @GUI::Widget {
+            layout: @GUI::HorizontalBoxLayout {
+                spacing: 16
+            }
+
+            @GUI::Label {
+                text: "Default view:"
+                text_alignment: "CenterLeft"
+                fixed_width: 70
+            }
+
+            @GUI::ComboBox {
+                name: "default_view"
+            }
         }
     }
 }

+ 2 - 0
Userland/Applications/CalendarSettings/CalendarSettingsWidget.h

@@ -18,6 +18,8 @@ public:
 
 private:
     CalendarSettingsWidget();
+    static constexpr Array<StringView, 2> const m_view_modes = { "Month"sv, "Year"sv };
 
     RefPtr<GUI::ComboBox> m_first_day_of_week_combobox;
+    RefPtr<GUI::ComboBox> m_default_view_combobox;
 };

+ 8 - 0
Userland/Libraries/LibGUI/Calendar.cpp

@@ -45,6 +45,14 @@ Calendar::Calendar(Core::DateTime date_time, Mode mode)
         }
     }
 
+    auto default_view = Config::read_string("Calendar"sv, "View"sv, "DefaultView"sv, "Month"sv);
+    if (default_view == "Year") {
+        m_mode = Year;
+        m_show_days = false;
+        m_show_year = true;
+        m_show_month_year = true;
+    }
+
     update_tiles(m_selected_date.year(), m_selected_date.month());
 }