D言語からObjective-CとCocoaAPIを使いたい

D言語から Alert を使用する

D言語から Mac の GUI を使用して Alert (NSAlert) を表示します。

suppressionButton の表示を追加しました。

alertのSample

サンプルコード

Powered by Prism

alert/dub.json

{
    "authors": ["hidhok"],
    "description": "A D/Objective-C Cocoa application.",
    "name": "alert",
    "dflags": ["-L-framework", "-LCocoa"],
    "sourceFiles": ["../cocoa/strings"]
}               
alert/source/app.d

/**
 *      alert/source/app.d     Version 0.0.5
 */
module alert.main;
import core.attribute : selector;
import std.conv : to;
import std.stdio : writeln;
import cocoa.strings;

void main() {    
    string alertStr  = "D言語からCocoaの操作";
    string alertInfo = "Alertが首尾よく表示されましたか?";
    string result = fAlert(alertStr, alertInfo);
    writeln(result, "されました。"); 
}

string fAlert(string msgText, string infoText) {
    NSAlert alert = NSAlert.alloc.init;
    alert.addButton("OK".toNs);
    alert.addButton("Cancel".toNs);
    alert.messageText(msgText.toNs);
    alert.information(infoText.toNs);
    alert.style(alertStyle.critical);

    if (!alert.suppressionButton.state == btnState.Off) {
        alert.suppressionButton.title = "次回からこのメッセージを表示しない".toNs;
        alert.showSuppressionButton = true;
    }

    if (alert.suppressionButton.state == btnState.On) 
        writeln("操作前 suppressionButton.state = btnState.On");
    else 
        writeln("操作前 suppressionButton.state = btnState.Off");

    NSInteger response = alert.run;

    writeln("操作後 suppressionButton.state = btnState.", alert.suppressionButton.state);

    string returnStr;
    switch (response) {
    case AlertReturn.OK:
        returnStr = "OK";
        break;
    case AlertReturn.Cancel:
        returnStr = "Cancel";
        break;
    default:
        returnStr = "Other";
        break;
    } 
    alert.release;
    return returnStr;
}

extern (Objective-C)
extern class NSAlert : NSObject {
    override static NSAlert alloc() @selector("alloc");
    override NSAlert init() @selector("init");
    void addButton(NSString) @selector("addButtonWithTitle:"); 
    void messageText(NSString) @selector("setMessageText:");
    void information(NSString) @selector("setInformativeText:");
    void style(NSUInteger) @selector("setAlertStyle:");
    NSInteger run() @selector("runModal");
    void showSuppressionButton(bool) @selector("setShowsSuppressionButton:");
    NSButton suppressionButton() @selector("suppressionButton");
    override void release() @selector("release");
}

enum AlertButton : NSInteger { first = 1000, second, third }   
enum AlertReturn : NSInteger { OK = 1000, Cancel, Other }
enum AlertStyle  : NSInteger { warning, informational, critical }
enum NSControlStateValue : NSInteger { On = 1, Off = 0, Mixed = -1 }
alias btnState = NSControlStateValue;

extern (Objective-C)
extern class NSButton {
    void title(NSString) @selector("setTitle:");
    NSControlStateValue state() @selector("state");
    void state(NSControlStateValue) @selector("setState:");
}               
cocoa/strings.d