Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add calendar persian weekdays and numbers feature #19

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions [email protected]/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js'
import {PersianDate} from './persianDate.js';
import {EventSource} from './eventSource.js';
import {getDayAccessibleName} from './utils/dateformat.js';
import {toPersianDigit} from './utils/numbers.js';

const SHOW_WEEKDATE_KEY = 'show-weekdate';

Expand All @@ -37,7 +38,7 @@ function _isWorkDay(date) {
return !days.includes(date.getDay().toString());
}

function _getCalendarDayAbbreviation(dayNumber) {
function _getCalendarDayAbbreviation(dayNumber, isPersian) {
let abbreviations = [
/* Translators: Calendar grid abbreviation for Sunday.
*
Expand All @@ -57,18 +58,30 @@ function _getCalendarDayAbbreviation(dayNumber) {
NC_('grid friday', 'F'),
/* Translators: Calendar grid abbreviation for Saturday */
NC_('grid saturday', 'S'),

'ی',
'د',
'س',
'چ',
'پ',
'ج',
'ش',
];
return Shell.util_translate_time_string(abbreviations[dayNumber]);
return isPersian ? abbreviations[dayNumber + 7] : Shell.util_translate_time_string(abbreviations[dayNumber]);
}

export const Calendar = GObject.registerClass({
Signals: {'selected-date-changed': {param_types: [GLib.DateTime.$gtype]}},
}, class Calendar extends St.Widget {
_init() {
_init(settings) {
// this._weekStart = Shell.util_get_week_start();
this._weekStart = 6;
this._settings = new Gio.Settings({schema_id: 'org.gnome.desktop.calendar'});

this.settings = settings;
this.usePersianWeekday = this.settings.get_boolean('calendar-weekday-persian-number');
this.usePersianDay = this.settings.get_boolean('calendar-day-persian-number');

this._settings = new Gio.Settings({schema_id: 'org.gnome.desktop.calendar'});
this._settings.connect(`changed::${SHOW_WEEKDATE_KEY}`, this._onSettingsChange.bind(this));
this._useWeekdate = this._settings.get_boolean(SHOW_WEEKDATE_KEY);

Expand Down Expand Up @@ -178,7 +191,7 @@ export const Calendar = GObject.registerClass({
for (let i = 0; i < 7; i++) {
// Could use iter.toLocaleFormat('%a') but that normally gives three characters
// and we want, ideally, a single character for e.g. S M T W T F S
let customDayAbbrev = _getCalendarDayAbbreviation(iter.getDay());
let customDayAbbrev = _getCalendarDayAbbreviation(iter.getDay(), this.usePersianWeekday);
let label = new St.Label({
style_class: 'calendar-day-heading',
text: customDayAbbrev,
Expand Down Expand Up @@ -316,7 +329,9 @@ export const Calendar = GObject.registerClass({
while (row < nRows) {
let button = new St.Button({
// xgettext:no-javascript-format
label: iter.getPersianDate().toString().replace(/\d/g, x => _(x)),
label: this.usePersianDay
? toPersianDigit(iter.getPersianDate())
: iter.getPersianDate().toString().replace(/\d/g, x => _(x)),
can_focus: true,
});
let rtl = button.get_text_direction() === Clutter.TextDirection.RTL;
Expand Down
4 changes: 2 additions & 2 deletions [email protected]/dateMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class DateMenuButton extends PanelMenu.Button {
hbox = new St.BoxLayout({name: 'calendarArea'});
bin.add_child(hbox);

this._calendar = new Calendar();
this._calendar = new Calendar(settings);
this._calendar.connect('selected-date-changed', (_calendar, datetime) => {
let date = _gDateTimeToDate(datetime);
this._eventsItem.setDate(date);
Expand Down Expand Up @@ -346,7 +346,7 @@ class DateMenuButton extends PanelMenu.Button {

_updateCalendarDisplay() {
let displayFormat = this.settings.get_string('panel-format');
let usePersianDigit = this.settings.get_boolean('number-to-persian');
let usePersianDigit = this.settings.get_boolean('top-panel-persian-number');
let date = new PersianDate();
this._calendarDisplay.set_text(
usePersianDigit
Expand Down
12 changes: 11 additions & 1 deletion [email protected]/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@ export default class PersianCalendar extends Extension {
this.enable();
});

this.settings.connect('changed::number-to-persian', () => {
this.settings.connect('changed::top-panel-persian-number', () => {
this.disable();
this.enable();
});

this.settings.connect('changed::calendar-weekday-persian-number', () => {
this.disable();
this.enable();
});

this.settings.connect('changed::calendar-day-persian-number', () => {
this.disable();
this.enable();
});
Expand Down
36 changes: 34 additions & 2 deletions [email protected]/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,50 @@ export default class PersianCalendarPreferences extends ExtensionPreferences {
group.add(toPersianRow);

const toPersian = new Gtk.Switch({
active: settings.get_boolean('number-to-persian'),
active: settings.get_boolean('top-panel-persian-number'),
valign: Gtk.Align.CENTER,
});
settings.bind(
'number-to-persian',
'top-panel-persian-number',
toPersian,
'active',
Gio.SettingsBindFlags.DEFAULT
);
toPersianRow.add_suffix(toPersian);
toPersianRow.activatable_widget = toPersian;

const persianWeekdayRow = new Adw.ActionRow({title: 'Use Persian Weekday'});
group.add(persianWeekdayRow);

const persianWeekday = new Gtk.Switch({
active: settings.get_boolean('calendar-weekday-persian-number'),
valign: Gtk.Align.CENTER,
});
settings.bind(
'calendar-weekday-persian-number',
persianWeekday,
'active',
Gio.SettingsBindFlags.DEFAULT
);
persianWeekdayRow.add_suffix(persianWeekday);
persianWeekdayRow.activatable_widget = persianWeekday;

const persianCalDayRow = new Adw.ActionRow({title: 'Use Persian Number in Calendar'});
group.add(persianCalDayRow);

const persianCalDay = new Gtk.Switch({
active: settings.get_boolean('calendar-day-persian-number'),
valign: Gtk.Align.CENTER,
});
settings.bind(
'calendar-day-persian-number',
persianCalDay,
'active',
Gio.SettingsBindFlags.DEFAULT
);
persianCalDayRow.add_suffix(persianCalDay);
persianCalDayRow.activatable_widget = persianCalDay;

// Event Switchs
const gregorianEventsRow = new Adw.ActionRow({title: 'Show Gregorian Events'});
group.add(gregorianEventsRow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@
<default>"d mmmm yyyy"</default>
<summary>Panel date format</summary>
</key>
<key name="number-to-persian" type="b">
<key name="top-panel-persian-number" type="b">
<default>true</default>
<summary>Use persian digit in top panel</summary>
</key>
<key name="calendar-weekday-persian-number" type="b">
<default>true</default>
<summary>Use persian digit in calendar weekday</summary>
</key>
<key name="calendar-day-persian-number" type="b">
<default>true</default>
<summary>Use persian digit in calendar day</summary>
</key>
<key name="gregorian-events-active" type="b">
<default>true</default>
<summary>Show Gregorian Events</summary>
Expand Down