Yab: How to get a window to close when mouse is clicked anywhere

I want my “About” window to have no tab on the top. I want it to close when the mouse is clicked anywhere. I have this currently, and it doesn’t work:

Also, I tried emailing @bbjimmy for access to Yab forum, but still waiting for a reply, and I want to get this code done, as I only have about 2 or 3 hours in the evening to work on my project. Thanks.
Ideas @michel

#! /bin/yab

// Open the main window
WINDOW OPEN 450,100 TO 900,800, "MainView", "MyProgram"
MENU "File", "About", "A", "MainView"
MENU "File", "Quit", "Q", "MainView"

inloop = true
about_open = false  // Track if the About window is open

while (inloop)
    msg$ = message$
    SWITCH msg$
    
    // Handle main window quit request
    CASE "MainView:_QuitRequested|"
        window close "MainView"
        inloop = false
        break
        
    // Handle Quit from menu
    CASE "MainView:File:Quit|"
        window close "MainView"
        inloop = false
        break
        
    // Handle About from menu
    CASE "MainView:File:About|"
        if about_open = false then
            about_open = true
            
            // Open the "About" window
            WINDOW OPEN 500,200 TO 700,300, "About", "About"
            
            // Set properties of the "About" window
            WINDOW SET "About", "look", "bordered"
            WINDOW SET "About", "feel", "modal-app"
            
            // Draw the message in the "About" window
            DRAW TEXT 20,50, "Click Anywhere to Exit 'About'", "About"
        end if
        break

    // Handle click anywhere in "About" window to close it
    CASE "About:_MouseDown|"
        window close "About"
        about_open = false
        break

    END SWITCH
WEND

#! /bin/yab

// Open the main window
WINDOW OPEN 450,100 TO 900,800, “MainView”, “MyProgram”
MENU “File”, “About”, “A”, “MainView”
MENU “File”, “Quit”, “Q”, “MainView”
dim mouse$(1)
inloop = true
about_open = false // Track if the About window is open

while (inloop)
msg$ = message$
SWITCH msg$

// Handle main window quit request
CASE "MainView:_QuitRequested|"
    window close "MainView"
    inloop = false
    break
    
// Handle Quit from menu
CASE "MainView:File:Quit|"
    window close "MainView"
    inloop = false
    break
    
// Handle About from menu
CASE "MainView:File:About|"
    if about_open = false then
        about_open = true
        
        // Open the "About" window
        WINDOW OPEN 500,200 TO 700,300, "About", "About"
        
        // Set properties of the "About" window
        WINDOW SET "About", "look", "bordered"
        WINDOW SET "About", "feel", "modal-app"
        
        // Draw the message in the "About" window
        DRAW TEXT 20,50, "Click Anywhere to Exit 'About'", "About"
    end if
    break


END SWITCH

// Handle click anywhere in “About” window to close it
x = window get “About” ,“Exists”
if x=1 then
GetMouse()
if mdown then
window close “About”
about_open = false

end if

end if
WEND

sub GetMouse()
n = token(mouse message$(“About”), mouse$(), “:”)
mx = val(mouse$(1))
my = val(mouse$(2))
mlmb = val(mouse$(3))
mmmb=val(mouse$(4))
mrmb=val(mouse$(5))
mdown=mlmb+mmmb+mrmb

end sub

1 Like

To avoid any program crashes, I would set a number variable to 1 as soon as the about window is opened and only activate the mouse query for the about window when it is opened. When the window is then closed, set the variable back to 0.

even though he has already done this here, related to the window

Do you know the besly knowledge base?

https://besly.de

There you will find many yab instructions.

1 Like

Why bother with a click? This one will close on mouseover

#!/yab

window open 100,100 to 200,200, "MainW",""
Window set "MainW", "Look", "Bordered"
option set "MainW", "HasFocus", 1
while (true)
	if mouse message$ <> "" then 
		window close "MainW"
		exit
	endif
wend
1 Like

This misses the point, the request was for the user to press a button while the mouse is over the window to close the help pindow, your solution would close the window any time the mouse is over the window.

1 Like

my solution uses
x = window get “About” ,“Exists"

note: x will be zero if the window is not opened and the following:

if x then

will be false

The then clause will not be executed as x is zero and will not be true.

1 Like

Indeed, and you answered that, so I don’t have to. I’m showing that the original request is not the only way to close something as ephemeral as an About box.

1 Like

Thanks for the input Michel! I’m going to try both of these solutions out tonight to see how it feels and reacts. As I’m learning Yab right now, I might as well get the experience coding both. :+1:

There’s always another way to do it. To make bbjimmy happy, here’s another way to do what you asked for. I quite often use something like this:

#! yab

window open 100, 100 to 200, 200,"MainW", ""
window set "MainW", "Look", "Bordered"
button -10,-10 to 110,110, "DontClickHereIDontWannaDie","", "MainW"
view 1,1 to 100,100, "HidetheButton", "MainW"
//draw your about text on the HidetheButton view 
while(true)
	a$ = message$
	if instr(a$, "DontClickHereIDontWannaDie") then
		window close "MainW"
		exit
	endif
wend

This relies on the fact that in yab, once you have created a button, you can slather ten views on top of it, it always remains a button. So I create a button larger than the window, which makes it invisible. Then I create a view on top of it for your About box text. Click anywhere and the button is activated.

2 Likes

Is there any way to alter this so that the user can click anywhere on the desktop to close the “About” window?

I tried setting mdown=mlmb but of course, it still relied on the if mdown then statement in the CASE to be true, meaning the action has to take place inside the window.

I also added another IF statement so that if the User closed the main window (here I call it “MainView” but they still had the “About” window open, it will close the “About” window too. Don’t need that “About” window hanging around just because they never clicked on it to close it. Gotta think about how users might use the application :stuck_out_tongue:

3 Likes

If you query the mouse and define here via an if query that if the user clicks, but the window behind it is not the about, then it is closed.

Here are a smal example (but works only if you select another windows):

#!/boot/system/bin/yab

screenWidth = peek("desktopwidth")
screenHeight = peek("desktopheight")

ProgramName$="Test"

//main variables

aboutopen=0

WINDOW OPEN screenWidth/2-100,screenHeight/2-100 to screenWidth/2+100,screenHeight/2+100, ProgramName$, "Test"
	MENU "App", "About", "A", ProgramName$
	MENU "App", "Quit", "Q", ProgramName$


//Main Loop
dim part$(1)
dim mouse$(1)
inloop = true
while(inloop)
	msg$ = message$

	if (split(msg$, part$(), ":|") > 2) then
		PartOne$=part$(1)
		PartTwo$=part$(2)
		PartThree$=part$(3)
	fi

	if (split(msg$, part$(), ":|") > 3) then
		PartFour$ = part$(4)
	fi
	
	if (split(msg$, part$(), ":|") > 4) then
		PartFife$ = part$(5)
	fi

	if (msg$ <> "") print msg$
	
	switch msg$
	
	case ProgramName$+":App:About|"
		if(aboutopen=0)then
			aboutopen=1
		else		
		endif
		WINDOW OPEN screenWidth/2-50,screenHeight/2-50 to screenWidth/2+50,screenHeight/2+50, "About", "About"	
		break	
	
	case ProgramName$+":App:Quit|"
	case ProgramName$+":_QuitRequested|"
		WINDOW CLOSE ProgramName$
		break
		
	case "About:_QuitRequested|"
		WINDOW CLOSE "About"
		break
								
	default:

	end switch
	
	wait 0.05
	
	mmsg$ = MOUSE MESSAGE$()
	
	if (mmsg$<>"" and mmsg$<>old_mmsg$) then
		old_mmsg$= mmsg$ 
		nx = split(mmsg$, mouse$(), ":")
		Viewname$=mouse$(1)
		Xmouse	= val(mouse$(1))
		Ymouse	= val(mouse$(2))
		BLmouse	= val(mouse$(3))
		BCmouse= val(mouse$(4))
		print "ViewName:"+mouse$(1)
		print "X:"+mouse$(2)
		print "Y:"+mouse$(3)
	endif
	
	if(aboutopen=1)then
		x=x+1
		print x
		if(x>=2)then
			if(mouse$(1)="About")then
			else
				WINDOW CLOSE "About"
				aboutopen=0
			endif
			x=0
		endif
	else
	endif
	
	switch (mmsg$)
	end switch
	
	if(window count<1) inloop = false

	sleep 0.1
wend

Strictly speaking, everything should close down with the exit command. But it is more elegant to close everything down yourself.

1 Like

When a window loses focus it gets a deactivate message, so you can look for this message and avoid to get mouse messages

CASE “About:_Deactivated”

1 Like

Perhaps setting it as a modal could have some misbehaviors, so if these happen, set about window as a normal one

1 Like