MrKai77 / DynamicNotchKit

Utilize the MacOS notch for enhanced user experiences.
MIT License
56 stars 3 forks source link

using DNK #6

Closed gitultra75848 closed 2 months ago

gitultra75848 commented 2 months ago

sup, i have this code so far

`import SwiftUI import DynamicNotchKit

let notch = DynamicNotchInfo( systemImage: "figure", title: "Figure", description: "Looks like a person" ) notch.show(for: 2)`

this is basically my first time using swift/swiftui so my bad if this is an easy fix. on "notch.show(for: 2)" it says Expressions are not allowed at the top level. and on "import DynamicNotchKit" it says No such module "DymanicNotchKit" (yeah, i added it as a dependancy.) how do i fix this?

MrKai77 commented 2 months ago

When you add DynamicNotchKit into the project, you will need to add the scheme itself as a target.

As for the Expressions are not allowed at the top level., it means that you cannot simply call DynamicNotchKit from the top level. Since you are using SwiftUI, you will need to put it inside a view.

Anyways, if this is your first time using Swift, I would highly recommend going through some tutorials to get used to Swift before trying to use DynamicNotchKit :)

gitultra75848 commented 2 months ago

my bad, i figured out how to fix the expression error after posting this. also, i've watched some swift tutorials after this and still can't figure out how to add "the scheme itself as a target", am i watching the wrong tutorial or? though, i do have the add target window up, and i don't really understand which to select, since originally i selected as a macos app. is that the wrong thing?

MrKai77 commented 2 months ago

Make sure that when adding the package, you have selected the target (sorry not scheme) as so:

image
gitultra75848 commented 2 months ago

okay, that fixed it, but of course with every good thing theres a bad thing, lol.. when i did that, this error came out of nowhere, i searched reddit, stackoverflow, hacking with swift, etc and cant find a fix that would suit this.

Screenshot 2024-05-10 at 5 22 13 PM

my bad if im asking too many questions.

MrKai77 commented 2 months ago

Since you are using SwiftUI, you will need to wrap it inside something like a button:

Button {
    let notch = DynamicNotchInfo(
        systemImage: "figure",
        title: "Figure",
        description: "Looks like a person"
    )
    notch.show(for: 2)
}, label: {
    Text("Show")
}

(there may be typos here)

gitultra75848 commented 2 months ago

er, yeah soo..

Screenshot 2024-05-10 at 5 39 12 PM

fun, is there a different way?

MrKai77 commented 2 months ago

Yes, you will need to wrap the button in a SwiftUI view

Check these (just from a quick search):

gitultra75848 commented 2 months ago

appears im not understanding something, neither of these really make me understand what im supposed to do here, do you mind explaining?

MrKai77 commented 2 months ago

It will look something like this.

import SwiftUI
import DynamicNotchKit

struct ContentView: View {
    var body: some View {
        Button {
            let notch = DynamicNotchInfo(
                systemImage: "figure",
                title: "Figure",
                description: "Looks like a person"
            )
            notch.show(for: 2)
        }, label: {
            Text("Show")
        }
    }
}

Again, I would highly recommend getting comfortable with using SwiftUI by itself before trying to learn how to use DynamicNotchKit :)

gitultra75848 commented 2 months ago

i got it working, but just a question. how do i stop it from sliding in from the left and make it emerge only from the notch?

MrKai77 commented 2 months ago

That shouldn't be happening.. would you be able to send your code?

gitultra75848 commented 2 months ago

yeah, it's really just normal code. just was testing, if you want i can send a screen recording too. summary; i made a project, imported DNK, made a playground in the project, put the code, and it didn't give me any errors except for this glitch. though, it doesn't happen 100% of the time, it happens randomly. more when it says "build (2)" and then runs, but it changes between the expected and unexpected behaviour.

`import SwiftUI import DynamicNotchKit

let notch = DynamicNotchInfo( systemImage: "sparkles", title: "testing", description: "working" ) notch.show(for: 2) `

MrKai77 commented 2 months ago

I haven't tried using DynamicNotchKit from a swift playground, but maybe it might work if you make it run asynchronously using DispatchQueue.main.async { <DynamicNotchKit code> }? But I'm not sure, since I never intended for this to be run from a playground.

gitultra75848 commented 2 months ago

awesome, it works now. question though, can i make it topmost above all windows? when i switch windows (stage manager) while it's showing, the window infront overlaps the dynamic notch and goes over it.

MrKai77 commented 2 months ago

Ohh that's interesting.. Right here, the dynamicNotch is set to be on the screensaver level (one of the highest levels for a window), so I'm not exactly sure why that would be happening.

This would be a super hacky solution atm, but try adding this right after initializing the dynamicNotch, but before showing it:

notch.windowController?.window?.level = window.Level(rawValue: NSWindow.Level.RawValue(CGShieldingWindowLevel()))

This would set the window to be on the highest possible level, which might fix the stage manager issue.

gitultra75848 commented 2 months ago

Ohh that's interesting.. Right here, the dynamicNotch is set to be on the screensaver level (one of the highest levels for a window), so I'm not exactly sure why that would be happening.

"This would be a super hacky solution atm, but try adding this right after initializing the dynamicNotch, but before showing it:"

notch.windowController?.window?.level = window.Level(rawValue: NSWindow.Level.RawValue(CGShieldingWindowLevel()))

This would set the window to be on the highest possible level, which might fix the stage manager issue.

and where would this go? before "notch.show(for: 2)"?

MrKai77 commented 2 months ago

Yup!

gitultra75848 commented 2 months ago

Yup!

alright, i'll try it.

gitultra75848 commented 2 months ago
Screenshot 2024-05-11 at 5 11 26 PM

hmm, doesn't seem to work. do i have to import something?

MrKai77 commented 2 months ago

Hmm.. try typing the windowController then the ., and see what autocompletion options come up. Maybe there's something similar to window that comes up?

gitultra75848 commented 2 months ago

Hmm.. try typing the windowController then the ., and see what autocompletion options come up. Maybe there's something similar to window that comes up?

like this?

import SwiftUI import DynamicNotchKit

DispatchQueue.main.async { let notch = DynamicNotchInfo( systemImage: "sparkles", title: "testing", description: "working" ) notch.windowController.window?.level = window.Level(rawValue: NSWindow.Level.RawValue(CGShieldingWindowLevel())) notch.show(for: 5) }

MrKai77 commented 2 months ago

seems good to me!

gitultra75848 commented 2 months ago

seems good to me!

mm, okay. it gives me this.

Screenshot 2024-05-11 at 5 27 09 PM
MrKai77 commented 2 months ago

Try chaining the optional using the ? and see what happens

gitultra75848 commented 2 months ago
Screenshot 2024-05-11 at 5 29 51 PM

you mean this?

MrKai77 commented 2 months ago

Yes, and then remove window?.level and see what autocompletion can give you as an equivalent

gitultra75848 commented 2 months ago
Screenshot 2024-05-11 at 5 33 24 PM

same thing.

MrKai77 commented 2 months ago

ohhh whoops I had a typo :P

It's NSWindow.Level, not window.Level

gitultra75848 commented 2 months ago
Screenshot 2024-05-11 at 5 36 47 PM
MrKai77 commented 2 months ago

Try NSWindow.Level(.init(CGShieldingWindowLevel())) instead of window.Level(rawValue: RawValue(CGShieldingWindowLevel()))

gitultra75848 commented 2 months ago
Screenshot 2024-05-11 at 5 40 13 PM

cheers!

gitultra75848 commented 2 months ago

Try NSWindow.Level(.init(CGShieldingWindowLevel())) instead of window.Level(rawValue: RawValue(CGShieldingWindowLevel()))

nevermind. 😄

https://github.com/MrKai77/DynamicNotchKit/assets/157659892/b164a12e-c4c9-47ee-98c3-4081e103c07c

MrKai77 commented 2 months ago

Nooo 😭

But it works correctly without stage manager when switching windows right? In that case, I'm confident that this is a MacOS bug.

gitultra75848 commented 2 months ago

Nooo 😭

But it works correctly without stage manager when switching windows right? In that case, I'm confident that this is a MacOS bug.

nope, doesn't work without stage manager either..

MrKai77 commented 2 months ago

Alright, then it might be something with the project being a playground? Maybe since the popup is treated as an app window rather than a popuver.

Try putting the code inside a SwiftUI button and activate it from there maybe?

gitultra75848 commented 2 months ago

Alright, then it might be something with the project being a playground? Maybe since the popup is treated as an app window rather than a popuver.

Try putting the code inside a SwiftUI button and activate it from there maybe?

what should i put it as instead of a project? like, do i put it in a project? a workspace? and what do i put the code in? contentview.swift or?

MrKai77 commented 2 months ago

Yes, the code would be put in ContentView (or any other SwiftUI view).

gitultra75848 commented 2 months ago

I haven't tried using DynamicNotchKit from a swift playground, but maybe it might work if you make it run asynchronously using DispatchQueue.main.async { <DynamicNotchKit code> }? But I'm not sure, since I never intended for this to be run from a playground.

quick question, is it possible to have the dynamic notch appear when my cursor goes into it? (similar to mediamate.)

MrKai77 commented 2 months ago

Yes, you can call notch.checkIfMouseIsInNotch() to check if the mouse is inside the notch.