Sending Player info to a window

Post a reply

Smilies
:D :) :( :o :-? 8) :lol: :x :P :oops: :cry: :evil: :roll: :wink:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Sending Player info to a window

Re: Sending Player info to a window

by marceros » Tue Aug 29, 2023 10:22 am

Thank you very much!

My problem was that I declared the variables with let or const instead of using var.

Your explanation solved my issue.

Re: Sending Player info to a window

by drakinite » Mon Aug 28, 2023 4:05 pm

I believe that init.js runs within the main window scope but inside of a code block. So the following code inside init.js:

Code: Select all

var xVar = 1;
window.xWindow = 2;
const xConst = 3;
let xLet = 4;
results in xVar and xWindow being accessible from the window scope, but not xConst and xLet, because const and let are scoped differently from var.

The screenshot shows devtools open on the main window on the left, and on a dialog window on the right.
Image

Re: Sending Player info to a window

by marceros » Mon Aug 28, 2023 4:24 am

Hi,

I'm sorry. I think I missed your answer from about a year ago.

I'd like to rephrase my question. I think I understand better what I'm missing.

I'm using the mainWindow._window to set variables from one secondary window that will be used in another secondary window. This works.

The problem is that I set several variables with initial values in the init.js but I've just realized that I don't know how to get their values.

What would be the way to do that?

Marcelo

Re: Sending Player info to a window

by drakinite » Thu Sep 22, 2022 10:18 am

Essentially, any script that's NOT running inside a separate dialog window is running inside the main window.

Examples of scripts that run inside the main window:
  • addon's init.js
  • actions.js
  • mainwindow.js
  • templates.js
Examples of scripts that do NOT run inside the main window:
  • addon's config.js
  • Anything inside the "dialogs" folder
Scripts inside the "controls" folder may run either inside the main window OR inside a dialog window, depending on where it's initiated from. For example, Tools -> Equalizer has several Slider controls which exist inside the Equalizer dialog window, but the volume bar at the bottom of the main window exist, as you can probably guess, inside the main window.

Re: Sending Player info to a window

by marceros » Wed Sep 21, 2022 5:43 pm

drakinite wrote: Sat Aug 20, 2022 2:19 pm There are many ways you can do it, but here's an example with custom events. You can pass data through the detail parameter of createNewCustomEvent(). See: https://developer.mozilla.org/en-US/doc ... ustomEvent

Main window:
Image

Dialog window:
Image

Quick explainer on the first two lines: app.dialogs.getMainWindow() returns a SharedWindow object (from our Delphi code), but its _window property gives you access to the regular JS Window object, allowing you to listen to custom events.

Regarding the playListPos shenanigans: I forgot to check that out. Let's see...
Thank you drakinite!

I've just started to try your suggestion. I have a question: where should I define the custom event variable and function so it is recognized in the _window property of the "Main window". I guess I mean to ask: where is the "main window" source where I should define the custom event? Is it in the init.js?

Thanks,
Marcelo

Re: Sending Player info to a window

by drakinite » Sat Aug 20, 2022 2:36 pm

marceros wrote: Fri Aug 12, 2022 1:01 pm Let's say I have 10 songs in the player and the fifth song (playListPos=6) is now playing. Then, I move the sixth song to the place before the playing song so the moved song got the fifth place in the list and the playing song got the sixth place. The playListPos now should show 7 but it still shows 6.

Then I realized that I was reading the playListPos outside the tracklist locked function. Once I moved it into the function, it started working good.

Thank you!
I assume you mean sixth song (playlistPos=5), since the list positions are zero indexed?

I can reproduce the problem, tracked as https://www.ventismedia.com/mantis/view.php?id=19319

Re: Sending Player info to a window

by drakinite » Sat Aug 20, 2022 2:19 pm

There are many ways you can do it, but here's an example with custom events. You can pass data through the detail parameter of createNewCustomEvent(). See: https://developer.mozilla.org/en-US/doc ... ustomEvent

Main window:
Image

Dialog window:
Image

Quick explainer on the first two lines: app.dialogs.getMainWindow() returns a SharedWindow object (from our Delphi code), but its _window property gives you access to the regular JS Window object, allowing you to listen to custom events.

Regarding the playListPos shenanigans: I forgot to check that out. Let's see...

Re: Sending Player info to a window

by marceros » Wed Aug 17, 2022 3:40 am

Hello!

My program looks stable and does the main logics of the job. Before I write the next question/problem, I'd like to explain in a line or two what the program does.

I added a menu in the MM5 main window that opens a second window. Following is the code in the actions_add.js

Code: Select all

actions.openDanceFloorDisplay = {
    title: _('Dance floor display') + '...',
    hotkeyAble: true,
    // icon: 'openDanceFloorDisplay',
    // disabled: uitools.notMediaListSelected,
    visible: window.uitools.getCanEdit,
    execute: async function () {
        var dlg = uitools.openDialog('dlgDanceFloorDisplay', {
			left: 100,
			top: 100,
			width: 500,
			height: 500,
            show: true,
            modal: false,
            title: _('Dance floor display'),
            // tracks: list
        });
    }
}
That window goes to a second screen and displays information (to the people in the dance floor) about the theme that is playing and the following themes. All the logic is done in the JS of the open dialog and that's the part that I'm happy with.

Now I need to control the dialog from outside - from the main MM menu. Let's say that the second screen is different than expected and I have to change some internal parameters that control the text size and the height of the lines. Or I might want to change the text of the first line (title) showing on the second screen.

I guess the question is how I send data to the dialog JS (dlgDanceFloorDisplay.js) and how I run an update function in that JS, everything initiated from a menu in the main MM window.

Thank you in advance,
Marcelo

Re: Sending Player info to a window

by marceros » Fri Aug 12, 2022 1:01 pm

I figured out how to use the 'change' event on the 'getSongList' method. I'm using the events with the parameter 'delete' and with no parameter and everything works fine besides the fact that it triggers the event several unnecessary times. I assume the triggers will be more accurate when the

But meanwhile, I found another strange behavior with the app.player.playListPos.

Let's say I have 10 songs in the player and the fifth song (playListPos=6) is now playing. Then, I move the sixth song to the place before the playing song so the moved song got the fifth place in the list and the playing song got the sixth place. The playListPos now should show 7 but it still shows 6.

Then I realized that I was reading the playListPos outside the tracklist locked function. Once I moved it into the function, it started working good.

Thank you!

Re: Sending Player info to a window

by marceros » Thu Jul 21, 2022 3:08 pm

Thank you!

I think I need to do some homework.

Re: Sending Player info to a window

by drakinite » Thu Jul 21, 2022 2:33 pm

marceros wrote: Thu Jul 21, 2022 2:14 pm And in that case, I have to set a loop where the list variable gets the new list and the "change event" does the rest.
No, no need for a loop. I was just giving a general coding suggestion. app.listen(app.player.getSongList(), ...) is still valid; but if you want to access thisSongList inside your event handler, you won't have to do app.player.getSongList() multiple times.

Code: Select all

app.listen(app.player.getSongList(), 'change', function (changeType) {
    if (changeType === undefined) {
        console.log('Song list has been rearranged');
    }
})

Re: Sending Player info to a window

by marceros » Thu Jul 21, 2022 2:14 pm

Maybe I understand now. So I'm actually checking the change in a list, not an event as I'm used to. Is it right?

And in that case, I have to set a loop where the list variable gets the new list and the "change event" does the rest.

Am I seeing it right?

Thank you!

Re: Sending Player info to a window

by drakinite » Thu Jul 21, 2022 2:00 pm

Yep. Though it may be a better idea to store the list as a variable first.

Code: Select all

let thisSongList = app.player.getSongList();
app.listen(thisSongList, 'change', function (changeType) {
	// do stuff
});
(I haven't tested that code, but it should work)

Re: Sending Player info to a window

by marceros » Thu Jul 21, 2022 11:14 am

Thank you for the response!

Can you show me how the workaround line looks?

Maybe something like:

app.listen(app.player.getSongList(), 'change', eventFunction);

Thanks,
Marcelo

Re: Sending Player info to a window

by Ludek » Thu Jul 21, 2022 8:02 am

Hi Marcelo,
thanks for reporting, it really fails to work for re-order and deletions.

To be fixed as https://www.ventismedia.com/mantis/view.php?id=19266

Workaround is to listen 'change' event on app.player.getSongList()
https://www.mediamonkey.com/docs/api/cl ... etSongList

Top