Patch updated:
Index: ClockMenulet.m
===================================================================
--- ClockMenulet.m (revisione 8428)
+++ ClockMenulet.m (copia locale)
@@ -27,38 +27,12 @@
#import <AppKit/NSButton.h>
#import <AppKit/NSFont.h>
#import <AppKit/NSWindow.h>
-
#import <Foundation/NSTimer.h>
#import <Foundation/NSInvocation.h>
-#import <Foundation/NSCalendarDate.h>
#import <Foundation/NSString.h>
#import <Foundation/NSBundle.h>
#import <Foundation/NSUserDefaults.h>
-static inline NSString *
-ShortNameOfDay(int day)
-{
- return [[[NSUserDefaults standardUserDefaults]
- objectForKey: NSShortWeekDayNameArray]
- objectAtIndex: day];
-}
-
-static inline NSString *
-AMPMStringForHour(int hour)
-{
- NSArray * AMPMArray = [[NSUserDefaults standardUserDefaults]
- objectForKey: NSAMPMDesignation];
-
- if (hour < 12)
- {
- return [AMPMArray objectAtIndex: 0];
- }
- else
- {
- return [AMPMArray objectAtIndex: 1];
- }
-}
-
@implementation ClockMenulet
- (void) buttonAction: (id) sender
@@ -121,7 +95,10 @@
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0
invocation: inv
repeats: YES];
-
+ gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
+ dateFormatter = [[NSDateFormatter alloc] init];
+ [dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm"];
+ shortDayName = [dateFormatter shortWeekdaySymbols];
[self updateClock];
}
@@ -130,47 +107,16 @@
- (void) updateClock
{
- NSCalendarDate * date;
- int newHour, newMinute, newDay;
-
- date = [NSCalendarDate calendarDate];
-
- // only redraw every minute
- newHour = [date hourOfDay];
- newMinute = [date minuteOfHour];
- newDay = [date dayOfWeek];
- if (hour != newHour || minute != newMinute || day != newDay)
- {
- BOOL useAmPmTime = [[NSUserDefaults standardUserDefaults]
- boolForKey: @"UseAMPMTimeIndication"];
-
- hour = newHour;
- minute = newMinute;
- day = newDay;
-
- if (useAmPmTime)
- {
- int h = hour;
-
- if (h == 0)
- {
- h = 12;
- }
- else if (h > 12)
- {
- h -= 12;
- }
-
- [view setTitle: [NSString stringWithFormat:
- _(@"%@ %d:%02d %@"), ShortNameOfDay(day), h, minute,
- AMPMStringForHour(hour)]];
- }
- else
- {
- [view setTitle: [NSString stringWithFormat: _(@"%@ %d:%02d"),
- ShortNameOfDay(day), hour, minute]];
- }
- }
+ NSDate * date = [NSDate date];
+ NSDateComponents *dateComponents = [gregorian
components:NSWeekdayCalendarUnit fromDate:date];
+
+ NSInteger weekDayNum = [dateComponents weekday];
+ NSString *strDate = [NSDateFormatter localizedStringFromDate:date
dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterShortStyle];
+ NSString *dayNum = [dateFormatter stringFromDate:date];
+ [view setTitle: [NSString stringWithFormat:@"%@ %@ %@", [shortDayName
objectAtIndex:weekDayNum], [dayNum substringToIndex:2], strDate]];
+
+ [gregorian release];
+ [dateFormatter release];
}
- (NSView *) menuletView
Index: ClockMenulet.h
===================================================================
--- ClockMenulet.h (revisione 8428)
+++ ClockMenulet.h (copia locale)
@@ -23,6 +23,8 @@
#import <Foundation/NSObject.h>
#import "../../EtoileMenulet.h"
+#import <Foundation/NSCalendar.h>
+#import <Foundation/NSDateFormatter.h>
@class NSButton, NSTimer, NSWindow;
@@ -31,8 +33,9 @@
NSTimer *timer;
NSButton *view;
NSWindow *calendarWindow;
-
- int hour, minute, day;
+ NSCalendar *gregorian;
+ NSDateFormatter *dateFormatter;
+ NSArray *shortDayName;
}
- (NSView *) menuletView;
> From: [email protected]
> Date: Wed, 5 Jun 2013 10:14:12 +0100
> To: [email protected]
> Subject: Re: [Etoile-dev] EtoileMenuServer patch
>
> Hi,
>
> On 3 Jun 2013, at 18:15, Slex Sangiuliano <[email protected]> wrote:
>
> > + NSCalendar *gregorian = [[NSCalendar alloc]
> > initWithCalendarIdentifier:NSGregorianCalendar];
> > + NSDateFormatter *form = [[NSDateFormatter alloc] init];
>
> Rather than creating and destroying these every update, it might be better to
> create them in -init, store them in an ivar, and reuse them.
>
> > + NSDate * date = [NSDate date];
> > + NSDateComponents *dateComponents = [gregorian
> > components:NSWeekdayCalendarUnit fromDate:date];
> > +
> > + NSInteger weekDayNum = [dateComponents weekday];
>
> Looks fine.
>
> > + [form setDateFormat:@"dd/MM/yyyy HH:mm"];
>
> Likewise, this setup should only need to be done once if you reuse form (I'd
> rename it to dateFormatter or something similarly expressive).
>
> > + NSArray *shortDayName = [form shortWeekdaySymbols];
>
> You could cache this in an ivar too, as it will only change if the current
> locale changes. In
>
> > + NSString *strDate = [NSDateFormatter localizedStringFromDate:date
> > dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterShortStyle];
> > + NSString *dayNum = [form stringFromDate:date];
> > + [view setTitle: [NSString stringWithFormat:@"%@ %@ %@", [shortDayName
> > objectAtIndex:weekDayNum], [dayNum substringToIndex:2], strDate]];
> > +
> > + [gregorian release];
> > + [form release];
>
> David
>
>
>
>
> -- Sent from my Cray X1
>
>
> _______________________________________________
> Etoile-dev mailing list
> [email protected]
> https://mail.gna.org/listinfo/etoile-dev
clock_menulaet_updated.patch
Description: Binary data
_______________________________________________ Etoile-dev mailing list [email protected] https://mail.gna.org/listinfo/etoile-dev
