Git Product home page Git Product logo

Comments (4)

rodrigocfd avatar rodrigocfd commented on June 6, 2024

The problem is that you're making a mess with DestroyWindow and WmDestroy. But in order to better understand and control what's going on, I divided your program into 3 files: main.go, WndMain.go and WndModal.go. Each one takes care of 1 thing.

  1. Program entry point: main.go
package main

import (
	"runtime"
)

func main() {
	runtime.LockOSThread()

	mainWindow := NewWndMain()
	mainWindow.Run()
}
  1. Main window: WndMain.go
package main

import (
	"github.com/rodrigocfd/windigo/ui"
	"github.com/rodrigocfd/windigo/win"
)

// WndMain is the main application window.
type WndMain struct {
	wnd     ui.WindowMain
	btnOpen ui.Button
}

// WndMain constructor.
func NewWndMain() *WndMain {
	wnd := ui.NewWindowMain(
		ui.WindowMainOpts().
			Title("Test").
			ClientArea(win.SIZE{Cx: 800, Cy: 600}),
	)

	mainWindow := &WndMain{
		wnd: wnd,
		btnOpen: ui.NewButton(
			wnd,
			ui.ButtonOpts().
				Position(win.POINT{X: 10, Y: 10}).
				Size(win.SIZE{Cx: 300, Cy: 300}).
				Text("Open Modal"),
		),
	}

	mainWindow.events()
	return mainWindow
}

// WndMain will run.
func (me *WndMain) Run() {
	me.wnd.RunAsMain()
}

// WndMain events are attached here.
func (me *WndMain) events() {
	me.btnOpen.On().BnClicked(func() {
		modal := NewWndModal()
		modal.Show(me.wnd)
	})
}
  1. Modal window: WndModal.go
package main

import (
	"github.com/rodrigocfd/windigo/ui"
	"github.com/rodrigocfd/windigo/win"
	"github.com/rodrigocfd/windigo/win/co"
)

// WndModal is the modal window, opened by WndMain.
type WndModal struct {
	wnd      ui.WindowModal
	btnClose ui.Button
	edit     ui.Edit
}

// WndModal constructor.
func NewWndModal() *WndModal {
	wnd := ui.NewWindowModal(
		ui.WindowModalOpts().
			Title("Modal").
			ClientArea(win.SIZE{Cx: 300, Cy: 300}),
	)

	modalWindow := &WndModal{
		wnd: wnd,
		btnClose: ui.NewButton(
			wnd,
			ui.ButtonOpts().
				Position(win.POINT{X: 10, Y: 10}).
				Size(win.SIZE{Cx: 100, Cy: 30}).
				Text("Close"),
		),
		edit: ui.NewEdit(
			wnd,
			ui.EditOpts().
				Position(win.POINT{X: 10, Y: 50}).
				Size(win.SIZE{Cx: 280, Cy: 20}).
				Text("Hello, world!"),
		),
	}

	modalWindow.events()
	return modalWindow
}

// WndModal will be shown; blocks until the modal is closed.
func (me *WndModal) Show(parent ui.AnyParent) {
	me.wnd.ShowModal(parent)
}

// WndModal events are attached here.
func (me *WndModal) events() {
	me.btnClose.On().BnClicked(func() {
		me.wnd.Hwnd().SendMessage(co.WM_CLOSE, 0, 0)
	})
}

Although longer, such code structure is much more scalable, allowing your program to grow in complexity while being maintainable.

from windigo.

Hoto-Cocoa avatar Hoto-Cocoa commented on June 6, 2024

Thanks for the advise.

Sorry for asking questions, but why this code not works? I think the message box should appear after modal closed, but It doesn't appear.

// WndMain events are attached here.
func (me *WndMain) events() {
	me.btnOpen.On().BnClicked(func() {
		modal := NewWndModal()
		modal.Show(me.wnd)
		me.wnd.Hwnd().MessageBox("Modal closed.", "Test", co.MB_ICONINFORMATION)
	})
}

from windigo.

rodrigocfd avatar rodrigocfd commented on June 6, 2024

This time you found a bug. The modal loop was not being terminated correctly in some cases. It's fixed now, thank you.

from windigo.

Hoto-Cocoa avatar Hoto-Cocoa commented on June 6, 2024

Thanks! I confirm this issue resolved.

from windigo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.