Toolbar and menu

Adding a menu to an nk window is accomplished using NkMenubarBegin(), NkMenuBeginLabel(), and NkMenuItemLabel(), among others. The only difficult step in setting up a menu is that we also need to add an appropriate layout for the bar and its items. It's important (in fact, mandatory) that the bar is in a layout where y=0, so we immediately add a new row layout using dynamic sizing with NkLayoutRowBegin(). Then, we push the cell size for this layout using NLayoutRowPush().

A menu item is opened using NkMenuBeginLabel() and we must check the return value for this function—0 means that the menu is hidden. If it returns a non-zero value then we should lay out the menu below the bar. We start a new dynamic row layout with a single column using NkLayoutRowDynamic() to contain each menu item. Each menu item is then added using NkMenuItemLabel() with the appropriate label string. The return value for this function indicates whether the item has been clicked. If we get a non-zero value, then we should action the item—as shown by the
Quit item. Lastly, if the menu is open, we must close it again with NkMenuEnd():

   nk.NkMenubarBegin(ctx)
nk.NkLayoutRowBegin(ctx, nk.LayoutStaticRow, 25, 3)
nk.NkLayoutRowPush(ctx, 45)
if nk.NkMenuBeginLabel(ctx, "File", nk.TextAlignLeft, nk.NkVec2(120, 200)) > 0 {
nk.NkLayoutRowDynamic(ctx, 25, 1)
nk.NkMenuItemLabel(ctx, "New", nk.TextAlignLeft)
if nk.NkMenuItemLabel(ctx, "Quit", nk.TextAlignLeft) > 0 {
win.SetShouldClose(true)
}

nk.NkMenuEnd(ctx)
}

...

Further menus (Edit and Help, for example) can simply be added by starting another block with NkMenuBeginLabel(). For the complete listing, you can see the code repository for this book: chapter9/gomail.

Adding a toolbar is less straightforward as the Nuklear toolkit has no direct toolbar support. We'll simulate this by adding a row of buttons that are of fixed size and left-aligned in the bar. To do this, we open a new static row layout, specifying the desired size of the buttons as the cell width (and the correct number of columns). We then add each button with NkButtonLabel() passing a button label. Ideally, we would use NkButtonImage(), but there are no standard toolbar icons available. We could package the required icons ourselves and load the images, but there's currently little support for loading an image from Go code; a proposal exists to add NkImageFromRgba(), but at the time of writing, this doesn't exist. Implementing that image loading is out of scope for this chapter:

   ...

toolbarHeight := float32(24)
nk.NkLayoutRowStatic(ctx, toolbarHeight, 78, 7)
nk.NkButtonLabel(ctx, "New")
nk.NkButtonLabel(ctx, "Reply")
nk.NkButtonLabel(ctx, "Reply All")

nk.NkButtonLabel(ctx, "Delete")

nk.NkButtonLabel(ctx, "Cut")
nk.NkButtonLabel(ctx, "Copy")
nk.NkButtonLabel(ctx, "Paste")

...

Each of these buttons returns, an int value, like the preceding menu items, that indicate whether it was clicked. We'll add the button handling in the next section, Communicating with the GUI. With this code in place, we see a complete user interface for our email browse window:

The completed layout of our compose window
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.14.245