darling icon indicating copy to clipboard operation
darling copied to clipboard

Simple Stopwatch App Works Reliably

Open TheBrokenRail opened this issue 4 years ago • 1 comments

Expected Result When you click Start, the stopwatch will always start.

Actual Result It doesn't always start, only sometimes.

Steps To Reproduce

#include <Cocoa/Cocoa.h>

@interface Window : NSWindow {
  NSTextField* label;
  NSButton* button;
  NSTimer* timer;
  int counter;
}
- (instancetype)init;
- (void)onButtonClick:(id)sender;
- (void)onTimerTick:(NSTimer*)timer;
- (BOOL)windowShouldClose:(id)sender;
@end

@implementation Window
- (instancetype)init {
  counter = 0;
  
  label = [[[NSTextField alloc] initWithFrame:NSMakeRect(10, 50, 210, 70)] autorelease];
  [label setStringValue:[NSString stringWithFormat:@"%.1f", (float)counter / 10]];
  //[label setStringValue:@"0.0"];
  [label setBezeled:NO];
  [label setDrawsBackground:NO];
  [label setEditable:NO];
  [label setSelectable:NO];
  [label setTextColor:[NSColor colorWithDeviceRed:0.117 green:0.565 blue:1.0 alpha:1.0]];
  [label setFont:[[NSFontManager sharedFontManager] convertFont:[[NSFontManager sharedFontManager] convertFont:[NSFont fontWithName:@"Arial" size:60] toHaveTrait:NSFontBoldTrait] toHaveTrait:NSFontItalicTrait]];

  button = [[[NSButton alloc] initWithFrame:NSMakeRect(10, 10, 90, 32)] autorelease];
  [button setAction:@selector(onButtonClick:)];
  [button setBezelStyle:NSBezelStyleRounded];
  [button setTitle:@"Start"];
  
  [super initWithContentRect:NSMakeRect(100, 100, 230, 130) styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:NO];
  [self setTitle:@"Label Example"];
  [[self contentView] addSubview:label];
  [[self contentView] addSubview:button];
  [self setIsVisible:YES];
  return self;
}

- (void)onButtonClick:(id)sender {
  if ([[button title]  isEqual: @"Start"]) {
    timer = [NSTimer timerWithTimeInterval:0.1f target:self selector:@selector(onTimerTick:) userInfo:self repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    [button setTitle:@"Stop"];
  } else {
    [timer invalidate];
    [button setTitle:@"Start"];
  }
}

- (void)onTimerTick:(NSTimer*)timer {
  [label setStringValue:[NSString stringWithFormat:@"%.1f", (float)++counter / 10]];
}

- (BOOL)windowShouldClose:(id)sender {
  [NSApp terminate:sender];
  return YES;
}
@end

int main(int argc, char* argv[]) {
  [NSApplication sharedApplication];
  [[[[Window alloc] init] autorelease] makeMainWindow];
  [NSApp run];
}
  1. Compile The Program
  2. Run The Program
  3. Start And Stop The Stopwatch Until It Has Been Started And The Numbers Aren't Changing

System Information Ubuntu 20.04

Software Version
Linux Kernel 5.8.0-50-generic
Darling 35534fcaa312b184145c95a543cf86c8b08d4e70

TheBrokenRail avatar May 28 '21 13:05 TheBrokenRail

Works for me.

mrolappe avatar Jul 31 '22 22:07 mrolappe