go-sciter icon indicating copy to clipboard operation
go-sciter copied to clipboard

How to Cmd+Q to quit app on MacOS?

Open AllenDang opened this issue 6 years ago • 15 comments

How to implement cmd+q to exit the app?

I think this should be supported automatically if I can add a menu, but after a few research (google, sciter forum, etc...) I still cannot find a clue about it.

Any help?

AllenDang avatar Apr 28 '18 11:04 AllenDang

Yes, there should be a main menu. It must be described in Apple's documentation: https://developer.apple.com/macos/human-interface-guidelines/menus/menu-bar-menus/#app-menu

pravic avatar Apr 28 '18 11:04 pravic

Thanks for the link. So I think you mean this is not implement inside sciter, right? Ok, I will take a look at cgo.

AllenDang avatar Apr 28 '18 12:04 AllenDang

Sciter allows you to create a native window, that's basically it about system integration (I mean things like message loops, system tray, app icons and universe of others).

The main idea was to bring Sciter (as UI) to an application, not vice versa. You are supposed to have an existing application in particular platform and integrate UI in it.

Sciter is a library, not a framework, that's why we have to implement integration (again, things like message loops) in other languages ourselves.

pravic avatar Apr 28 '18 12:04 pravic

Also note that I am, personally, just a maintainer of bindings, so take my opinion with a grain of salt. Better discuss or ask such things on the official forum, where Andrew @c-smile (the author) answers officially.

pravic avatar Apr 28 '18 12:04 pravic

This event handler will catch CMD+Q (on Mac) or CTRL+Q (on Win/Lin) and close the window:

event keydown (evt) {
   if( evt.shortcutKey && (evt.keyCode == Event.VK_Q) )
   {
      view.close();
      return true; // consume the event.
   }  
}

If that's main window then it will close the application.

c-smile avatar Apr 28 '18 13:04 c-smile

@pravic

In go, we have options to integrate c code using cgo. Which already used by go-sciter already.

To add native mac menu, we should add a os restricted function like addMacMenubar.

Here is small example of menu add to menubar using cgo and cocoa.

Add menu to mac os app

Would like to get your opinion and hopping for positive response.

mchampaneri avatar Apr 25 '20 14:04 mchampaneri

@mchampaneri Constructing the application menu in runtime - interesting! I thought that it is possible only via static menu resources.

Perhaps, I'll take this idea for rust-sciter as well.

pravic avatar Apr 25 '20 16:04 pravic

@mchampaneri by the way, your examples are great! Have you considered placing them under github.com/sciter-sdk organization? You will be the author as usually, but the repository will be more visible for go-sciter users. What do you think?

pravic avatar Apr 26 '20 05:04 pravic

@pravic it will be great pleasure for me.

mchampaneri avatar Apr 26 '20 06:04 mchampaneri

Just an idea: if system menu is required then I think it makes sense to make it as native behaviors. So anyone can define:

<head>
    <menu.system>
         <li accesskey="... caption="File">
             <menu>
                  <li accesskey="..." caption="Open" />
                  <li accesskey="... caption="Quit"/>
            </menu>
         </li> 
    </menu>   
</head>
<body>
   ....
</body>

And css:

head > menu.system { behavior:system-menu; }

The system-menu behavior implementation is different for different OSes.

Another option is to provide some native method like view.defineMenu()

c-smile avatar Apr 26 '20 15:04 c-smile

@c-smile I tried the way you told. Yes its a feasible one.

<script type='text/tiscript'>
                function self.ready(){
                    view.LoadMenu()
                    view.StartApp()
                }
</script>
win.DefineFunction("LoadMenu", func(...*sciter.Value) *sciter.Value {
		fmt.Println("ok")
		C.LoadMenu()
		return sciter.NewValue("")
	})

win.DefineFunction("StartApp", func(...*sciter.Value) *sciter.Value {
		win.Show()
		return sciter.NewValue("")
	})

There is just one problem I am facing. When app launches first time mac menu does not response to click, but when I reselect the app it works normally. It's not related to view.LoadMenu(). It was also occurred when I called the function from native go code as well.

I

mchampaneri avatar Apr 27 '20 05:04 mchampaneri

@c-smile

Defining function this way might force to Implement view.defineMenu() for sdk wrappers in every language.

In sciter-forum, I have seen a post regards python. Some one has written that python can not make c-language call. If python can not make a c-lang call, We will not achieve what we want.

Sciter-sdk itself is in c and so I guess It will be better option if sciter-sdk itself do this, So other wrapper just have to pass on data for runtime menu.

What do you think?

mchampaneri avatar Apr 27 '20 06:04 mchampaneri

@mchampaneri

Some one has written that python can not make c-language call. If python can not make a c-lang call, We will not achieve what we want.

Why? Python has ctypes for cross-language calls: https://github.com/sciter-sdk/pysciter/blob/master/sciter/platform.py#L95

pravic avatar Apr 27 '20 09:04 pravic

@pravic

Thanks for sharing code. I am not much aware about python. It seems like sciter already using cocoa. We just have to add menu in case of mac.

@c-smile

This is just an opinion.

We should go with native menu for Mac OS X and Ubuntu like Oses which have main menu outside application window which needs native support. For windows or other Oses where main menu is within app window itself does not require native support.

I am agree with you @c-smile . We should call Menu binding from view, by that way frontend code of application will be same regardless of Os in context of main menu.

mchampaneri avatar Apr 27 '20 11:04 mchampaneri

@c-smile

There is one more requirement to make menu working. Build has to be inside Contents/MacOS folder. Otherwise it will have this issue.

There is just one problem I am facing. When app launches first time mac menu does not response to click, but when I reselect the app it works normally. It's not related to view.LoadMenu(). It was also occurred when I called the function from native go code as well.

mchampaneri avatar May 02 '20 06:05 mchampaneri