FelixKratz / SketchyBar

A highly customizable macOS status bar replacement
https://felixkratz.github.io/SketchyBar/
GNU General Public License v3.0
6.56k stars 95 forks source link

the build in event media_change cannot detect music stop (music app exit)? #587

Closed marsjane closed 3 months ago

marsjane commented 3 months ago

I use the alias-item to display lyrics which is build-in function for neteasemusic app, I use following config:

local colors = require("colors")
local icons = require("icons")
local settings = require("settings")

local whitelist = { 
  ["NetEaseMusic"] = true,
};

local neteasem = sbar.add("alias", "NetEaseMusic,Item-0", {
  position = "right",
  drawing = false,
  updates = true,
  -- width = settings.group_paddings
  background = {
    drawing = false,
    color = colors.transparent,
    border_color = colors.transparent,
  },
})

neteasem:subscribe("media_change", function(env)
  if whitelist[env.INFO.app] then
    local drawing = (env.INFO.state == "playing" or env.INFO.state == "paused")
    -- print("state")
    -- print(env.INFO.state)
    -- print(drawing)
    neteasem:set(
    { drawing = drawing, background = {
      color = colors.transparent,
      border_color = colors.white,
      height = 32,
      drawing = drawing,
      border_width = 1
    }})
  else
    neteasem:set(
    { drawing = false, background = {
      drawing = false,
      color = colors.transparent,
      border_color = colors.transparent,
    }})

  end
end
)

Now the bug is that: when pc starts, this item is empty which is expected, and when start/pause neteasemusic, the lyric alias item will then show as expect, BUT when I exit the neteasemusic app, the item position will show a blank while box which seems the drawing parameters not update to false as expect, I don't know which part is wrong, can you help? FYI the three print lines can print as expected when I start/pause music, but didn't print anything when I exit neteasemusic app, I guess media_change didn't triggered when the music totally stops(app exit), is it true? If so do you have any good idea to let this item totally disappear when app exit?

PhrantiK commented 3 months ago

space_windows_change should detect the window being closed:

https://felixkratz.github.io/SketchyBar/config/events

untested but something like this may work:

local function update_drawing(drawing)
  neteasem:set({
    drawing = drawing,
    background = {
      color = colors.transparent,
      border_color = colors.white,
      height = 32,
      drawing = drawing,
      border_width = 1
    }
  })
end

neteasem:subscribe("media_change", function(env)
  if whitelist[env.INFO.app] then
    current_app = env.INFO.app
    local drawing = (env.INFO.state == "playing" or env.INFO.state == "paused")
    update_drawing(drawing)
  else
    current_app = nil
    update_drawing(false)
  end
end)

neteasem:subscribe("space_windows_change", function(env)
  if current_app and not env.INFO.apps[current_app] then
    current_app = nil
    update_drawing(false)
  end
end)
marsjane commented 3 months ago

Thanks for the reply, I test it and think it has one issue that if the space_windows_change happens in other space, the item will also disappear, I think I will use the transparent border as a workaround~