* * *

Author Topic: macOS Extensions  (Read 1825 times)

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1922
macOS Extensions
« on: April 15, 2018, 08:03:27 pm »
A little bit more of native macOS in LCL apps:
http://wiki.freepascal.org/macosext

ChrisR

  • Jr. Member
  • **
  • Posts: 58
Re: macOS Extensions
« Reply #1 on: April 24, 2018, 03:56:45 pm »
These features are really useful. Dark mode is great for radiology, I have employed your code into my Lazarus based MRI viewer
 https://github.com/neurolabusc/MRIcroGL/issues/20

I think this does a nice job of encapsulating useful MacOS features without interfering with the core LCL features and 'write once, compile everywhere' nature.

I do have two suggestions. I understand you are busy implementing core LCL Cocoa features, so these are low priority (want, not need). However, MacOS does provide two handy features that seem like a natural fit for your component:

1.) NSAlert is like a modal message, but it is attached to one specific form. This is great when you are using multiple applications, as it is obvious which form is the parent. A handy feature for your component would be to present a string of text for the user.

2.) NSUserNotification can be used for a lot of features. However, the easiest to implement for Lazrus, and a hugely influential role is to report a brief message that does not require any user intervention. This will tell the user something is happening, without requiring a modal response. For Lazarus, providing a string text message and a delay would be great.

Below are two XCode snippets to demonstrate these. These are both from my open source project
  https://github.com/neurolabusc/MRIcro
This is available in compiled form with MRIcroGL
  https://github.com/neurolabusc/MRIcroGL/releases

The NSAlert is shown when you File/Open an image and then press the 'i' button, the Notification is shown when a user has the "Only initial volumes" preference checked and they load a huge 4D dataset: in this case only the first 1 Gb of images are loaded, and the notification tells the user that they are only seeing part of the dataset (and how to remedy this).



- (IBAction)notifyNotAllVolumesLoaded: (int) loadedVols RawVols: (int) rawVols;
{
    NSUserNotification *notification = [[NSUserNotification alloc] init];
    notification.title = [NSString stringWithFormat:@"Loaded %d of %d volumes", loadedVols, rawVols];
    notification.informativeText = @"Reason: The preference 'Only initial volumes' is selected";
    notification.soundName = NULL;
    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
    [NSTimer scheduledTimerWithTimeInterval: 4.5  target:self selector: @selector(closePopup) userInfo:self repeats:NO];
}

- (IBAction)infoClick:(id)sender { //
    NSString *message = [niiGL getHeaderInfo];
    NSBeginAlertSheet(@"Header Information", @"OK",NULL,NULL,theWindow, self,
                      NULL, NULL, NULL,
                      @"%@"
                      , message
                      );
}

ChrisR

  • Jr. Member
  • **
  • Posts: 58
Re: macOS Extensions
« Reply #2 on: April 24, 2018, 04:39:31 pm »
By the way, I note that there is other available code for NSUserNotification

  https://forum.lazarus.freepascal.org/index.php/topic,23977.15.html

To get the code to compile with Cocoa SVN 1.9, I needed to change the Uses, so "CarbonProc" becomes "CocoaUtils". I still think it might be nice to use your component as a wrapper for this function, to keep the Cocoa-specific features available.

ChrisR

  • Jr. Member
  • **
  • Posts: 58
Re: macOS Extensions
« Reply #3 on: April 26, 2018, 03:00:28 pm »
I have added a feature request to add notifications and alert sheets. The issue report provides a sample project that illustrates both
   https://bugs.freepascal.org/view.php?id=33655

The code displays alert sheets as follows

procedure ShowAlertSheet(FormHandle: HWND; const TitleStr, MessageStr: string);
var
  tNSStr,mNSStr, okNSStr, fNSStr: NSString;
  theWindow : CocoaAll.NSWindow;
  theID : id;
begin
  theID := NSView(FormHandle).window;
  theWindow := NSView(FormHandle).window;
  tNSStr := NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, TitleStr, kCFStringEncodingUTF8));//title
  mNSStr := NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, MessageStr, kCFStringEncodingUTF8));//message
  okNSStr := NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, 'OK', kCFStringEncodingUTF8));//button caption
  fNSStr := NSString(CFStringCreateWithPascalString(kCFAllocatorDefault, '%@', kCFStringEncodingUTF8));//format
  NSBeginAlertSheet(tNSStr,okNSStr,nil,nil,theWindow,theID,nil,nil,nil,fNSStr, [mNSStr]);
end;

Trenatos

  • Sr. Member
  • ****
  • Posts: 373
  • Software developer - Open source contributor
    • MarcusFernstrom.com
Re: macOS Extensions
« Reply #4 on: April 26, 2018, 05:08:18 pm »
This is great, thanks skalogryz for working on this stuff and sharing with us  :)

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1922
Re: macOS Extensions
« Reply #5 on: April 28, 2018, 02:40:35 am »
There's no intention to replace LCL features, but rather to extend them.

So for the following feature
1.) NSAlert is like a modal message, but it is attached to one specific form. This is great when you are using multiple applications, as it is obvious which form is the parent. A handy feature for your component would be to present a string of text for the user.

you could try using this:
Code: Pascal  [Select]
  1. uses
  2.   ..
  3.   ,LCLType  // for idDialogXXX constants
  4.   ,LCLIntf // for PromptUser()
  5.   ..
  6.  
  7.  
  8. procedure TForm1.Button1Click(Sender: TObject);
  9. var
  10.   res : Integer;
  11. begin
  12.   res := PromptUser(
  13.     'Cocoa Prompt User (NSAlert)'
  14.    ,'Please confirm that you''re good'
  15.    ,idDialogConfirm
  16.    ,[idButtonOk, idButtonCancel]
  17.    , 0
  18.    , mrCancel
  19.   );
  20.   if res = mrOk
  21.     then Caption := 'Confirmed'
  22.     else caption:='not';
  23. end;
  24.  

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1922
Re: macOS Extensions
« Reply #6 on: April 28, 2018, 03:02:48 am »
var
  theWindow : CocoaAll.NSWindow;
later patches removed NSWindow redeclaration within CocoaPrivate. No need to type out "CocoaAll." anymore.

ChrisR

  • Jr. Member
  • **
  • Posts: 58
Re: macOS Extensions
« Reply #7 on: April 28, 2018, 05:38:07 pm »
skalogryz
 The NSAlertSheet is different from ShowDialog and other meal windows - it is attached to the FOrm's window, rather than being an independent window. This is elegant for modal dialogs when you have several applications open, as it is explicit which form the AlertSheet belongs to.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1922
Re: macOS Extensions
« Reply #8 on: April 28, 2018, 10:06:32 pm »
http://wiki.freepascal.org/macOS_extensions#PromptUserSheet

you need to get the latest version of macOS extensions and Cocoa widgetset

Alextp

  • Hero Member
  • *****
  • Posts: 635
    • UVviewsoft
Re: macOS Extensions
« Reply #9 on: May 03, 2018, 09:05:53 am »
For CudaText, it is good to have component for Touch Bar.
https://developer.apple.com/macos/touch-bar/
« Last Edit: May 03, 2018, 09:07:28 am by Alextp »

Frank

  • Jr. Member
  • **
  • Posts: 52
Re: macOS Extensions
« Reply #10 on: May 11, 2018, 12:52:07 pm »
Is there any plan to add this to Lazarus' trunk ?

I have seen other macOS Cocoa specific (visual) things that would be nice to seamlessly add to Lazarus...

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1922
Re: macOS Extensions
« Reply #11 on: May 11, 2018, 02:18:21 pm »
Is there any plan to add this to Lazarus' trunk ?

I have seen other macOS Cocoa specific (visual) things that would be nice to seamlessly add to Lazarus...
very unlikely.

It pretty much confronts the ide of LCL-cross platform solutions.
The proper way of adding a feature into LCL is to have it support by at least 2-3 platforms.

These features are macOS specific. Not sure if Qt on Mac would allow the similar (macos specific) adjustments.

The example that could be added to LCL is system notifications.
I.e. creating a cross-platform platform notification component could be helpful. (because such notifications do exist in modern Windows and OSX).



 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus