SageTV Community  

Go Back   SageTV Community > SageTV Development and Customizations > SageTV Studio
Forum Rules FAQs Community Downloads Today's Posts Search

Notices

SageTV Studio Discussion related to the SageTV Studio application produced by SageTV. Questions, issues, problems, suggestions, etc. relating to the Studio software application should be posted here.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-02-2010, 12:37 PM
evilpenguin's Avatar
evilpenguin evilpenguin is offline
SageTVaholic
 
Join Date: Aug 2003
Location: Seattle, WA
Posts: 3,696
A way to check what menu referenced code is running under?

Is there a built in way to check what menu referenced code is running under? For example, if i'm in the SageRecordings w/ Optional Preview & AutoCategories menu I want some referenced code to act one way, but if i'm under the MediaFileList I want it to act a different way.
__________________
Clients: 1xHD200 Connected to 50" TH-50PZ750U Plasma
Server : Shuttle SFF SSH55J2 w/ Win7 Home, SageTV v7, Core i3 540, 2GB RAM, 30GB SSD for OS, 1.5TB+2x1TB WDGP for Recordings, BluRay, 2xHDHR, 1xFirewire
SageTV : PlayOn, SJQ, MediaShrink, Comskip, Jetty, Web Client, BMT


Having a problem? Don't forget to include a log! (Instructions for: PlayOn For SageTV v1.5, MediaShrink)
Reply With Quote
  #2  
Old 12-02-2010, 01:30 PM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
Quote:
Originally Posted by evilpenguin View Post
Is there a built in way to check what menu referenced code is running under? For example, if i'm in the SageRecordings w/ Optional Preview & AutoCategories menu I want some referenced code to act one way, but if i'm under the MediaFileList I want it to act a different way.
Yup I do this in diamond all the time. Look at the widget API. I call exactly what you want like so.

Code:
GetWidgetName(GetCurrentMenuWidget())
That will get you the current name of the current menu in use. That returns the text as so "SageRecordings w/ Optional Preview & AutoCategories" or you can be safer and do it using the WidgetSymbol instead of the name so if the name changes you are still okay.


Code:
GetWidgetSymbol(GetCurrentMenuWidget())
Which will return the symbol name,

cheers

Last edited by PLUCKYHD; 12-02-2010 at 01:33 PM.
Reply With Quote
  #3  
Old 12-02-2010, 01:57 PM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
While Plucky's suggestion will work, I'd argue that it's not good design to make the referenced code's behavior dependent on the identity of the referencing menu. This would be analogous to making a Java method behave differently depending on the name of the calling class. The right way to do it in Java would be to pass in a parameter to the method specifying which behavior to use. Then it's up to the caller to say explicitly what it wants, instead of leaving it up to the method to sneak a peek at the caller and guess what it wants.

The same sort of discipline is even more important in widget code, where formal function definitions don't exist and the temptation to produce incomprehensible spaghetti code is much greater. If your shared code can do its job more than one way, then pass in a variable saying explicitly which way you want it done, and document that requirement in a comment at the entry point to the shared code. Take seriously the encapsulation of shared code and the proper division of labor between caller and callee by making everything explicit at the point of interface, instead of burying hidden contextual dependencies deep inside the shared code.
__________________
-- Greg
Reply With Quote
  #4  
Old 12-02-2010, 02:13 PM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
Quote:
Originally Posted by GKusnick View Post
While Plucky's suggestion will work, I'd argue that it's not good design to make the referenced code's behavior dependent on the identity of the referencing menu. This would be analogous to making a Java method behave differently depending on the name of the calling class. The right way to do it in Java would be to pass in a parameter to the method specifying which behavior to use. Then it's up to the caller to say explicitly what it wants, instead of leaving it up to the method to sneak a peek at the caller and guess what it wants.
As far as I know EP is not coding Java but strictly in studio and I see no reason or wrong with using the Widget API for this use case especially for a Studio only coding addin (which I think he is doing). Yes you could go pass an attribute at every menu to get what you want passed but that is overkill and the widget api I believe was created to be used. I don't pretend to know everything about coding but I don't think it is bad design within studio and actually the only way to get the current menu symbol/name within studio short of creating an attribute that gets set at every menu.

While it gets buried and may become "spaghetti code" to me putting comments in studio is even more spaghettie code. If there was a comment widget it would make more since but to me there really isn't much of a way to avoid some spaghettie type code within studio.

Last edited by PLUCKYHD; 12-02-2010 at 02:17 PM.
Reply With Quote
  #5  
Old 12-02-2010, 03:07 PM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
The fact that Studio allows a looser coding style doesn't make it a good idea to practice that style. And with all due respect, Plucky, the fact that you don't see a problem doesn't mean there isn't one. It was exactly this sort of stylistic abuse -- gotos, global variables, hidden dependencies, and the like -- that drove the evolution of programming languages from the Fortran-like languages of the 1950s to the Algol-like languages of the 60s and 70s, with their modules, encapsulation, explicit function parameters, and so on.

If we don't want to repeat the errors of the past then it behooves us to learn those lessons and adopt a coding style -- even in Studio -- that approximates as closely as possible the modular, disciplined approach of Algol and its successors rather than just letting it all hang out Fortran-fashion.

To make it more concrete, let's say you have some shared code that can display information in a couple of different ways. Sometimes you want a list view, other times you want a grid view. You could do it as you suggested:

Code:
If GetWidgetSymbol(GetCurrentMenuWidget())
  > "GFDHF-12345"
    (widgets for list view)
  > "MNBMN-67890"
    (widgets for grid view)
Or you could do something like this instead:

Code:
"REM Display list view or grid view depending on the value of ViewStyle."
  If ViewStyle
    > "List"
      (widgets for list view)
    > "Grid"
      (widgets for grid view)
Of course you're free to code however you want. But I think you'd have a tough time arguing that the first example is somehow clearer or better or a more appropriate use of Studio capabilities than the second.
__________________
-- Greg
Reply With Quote
  #6  
Old 12-02-2010, 03:10 PM
jaminben jaminben is offline
Sage Icon
 
Join Date: Sep 2007
Location: Norwich, UK
Posts: 1,754
Send a message via MSN to jaminben
I'll poke my nose in here and go with Plucky on this one as I'm pretty sure this is how Sage do it themselves when searching for or linking to/from menu items.... e.g when returning to a menu after playback has finished from within the MediaPlayer OSD.

Of course take what I say with a pinch of salt as Greg (and Plucky) have light years more expirence than myself
__________________
Server - Win7 64bit, 2.4Ghz Intel Core 2 Duo, TBS 6284 PCI-E Quad DVB-T2 Tuner, 3 x HD200 & 1 x HD300 extenders
Reply With Quote
  #7  
Old 12-02-2010, 03:18 PM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
Quote:
Originally Posted by GKusnick View Post

Code:
If GetWidgetSymbol(GetCurrentMenuWidget())
  > "GFDHF-12345"
    (widgets for list view)
  > "MNBMN-67890"
    (widgets for grid view)
Or you could do something like this instead:

Code:
"REM Display list view or grid view depending on the value of ViewStyle."
  If ViewStyle
    > "List"
      (widgets for list view)
    > "Grid"
      (widgets for grid view)
Of course you're free to code however you want. But I think you'd have a tough time arguing that the first example is somehow clearer or better or a more appropriate use of Studio capabilities than the second.
How is the first example not clear Given you know the api you are doing the same thing? A simple Comment line gives you the same explanation.

Code:
"REM Get The current menu name and perform action needec"
If GetWidgetName(GetCurrentMenuWidget())
  > "VideoList"
    (widgets for videolist)
  > "SageRecordings w/ Optional Preview & AutoCategories"
    (widgets recordings)
I would argue in fact that is clearer to me as if you are setting attributes for one you have to make sure you add your attribute to every menu item you want to use and for to another code looker has to go to each menu item and find that attribute to figure out what is calling our what. Where as using the widget api you are explicitly calling out the menu widgets name or symbol which is easily found by anyone. Granted you could search for the attribute widget but I don't see how that is any clearer.

Also not trying to argue just discuss
Reply With Quote
  #8  
Old 12-02-2010, 03:48 PM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
The most important difference between the two examples is that in the first case the shared code is making assumptions about who's calling it and what they want. It not a subroutine's job to make that sort of assumption. It should just provide a well-documented service and let callers decide for themselves which options they want. That's what the second example is meant to convey. All the knowledge about which option to invoke is communicated explicitly through the ViewStyle parameter, rather than implicitly by calling context. Again, this is exactly why function parameters were invented, to allow clean division of labor between caller and callee, so the callee doesn't have to make any assumptions about who's calling it.

As a secondary point, I'd also argue that discriminating based on menu name is extremely bad form. Suppose down the road somebody makes some changes to that menu and decides to rename it since the old name no longer describes its function adequately. Menu names are supposed to be like comments; they communicate something to the human reader but aren't meant to have programmatic meaning. By breaking that rule, you've created a booby trap that will blow up unexpectedly on somebody who's unaware that the menu name is embedded elsewhere in the code and must not be changed.
__________________
-- Greg
Reply With Quote
  #9  
Old 12-02-2010, 05:18 PM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
Quote:
Originally Posted by GKusnick View Post
As a secondary point, I'd also argue that discriminating based on menu name is extremely bad form. Suppose down the road somebody makes some changes to that menu and decides to rename it since the old name no longer describes its function adequately. Menu names are supposed to be like comments; they communicate something to the human reader but aren't meant to have programmatic meaning. By breaking that rule, you've created a booby trap that will blow up unexpectedly on somebody who's unaware that the menu name is embedded elsewhere in the code and must not be changed.
That is why I also suggested the Symbol call since that will not change even if the name changes, I also assumed he was calling default menu's which won't change much or shouldn't.

I agree with your above statement in general I just don't think allot of those concepts pass on in Studio. Allot of the reason is like Ben pointed out most devs that are new will look at the default and copy how it is done there. I didn't search myself but Ben says that call is used so most will use that as an example.

In the end studio is an odd beast in my opinion while it does function well somethings are allot different then in most sceneraios and I always code in studio which ever way is going to put the least amount of code into studio and try to do most of it in java.
Reply With Quote
  #10  
Old 12-02-2010, 06:20 PM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
Quote:
Originally Posted by PLUCKYHD View Post
Allot of the reason is like Ben pointed out most devs that are new will look at the default and copy how it is done there.
That may well be true but that needn't stop us from encouraging newbies to develop better coding habits.
__________________
-- Greg
Reply With Quote
  #11  
Old 12-02-2010, 07:07 PM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
Quote:
Originally Posted by GKusnick View Post
That may well be true but that needn't stop us from encouraging newbies to develop better coding habits.
Fair enough
Reply With Quote
  #12  
Old 12-03-2010, 05:57 AM
Opus4's Avatar
Opus4 Opus4 is offline
Administrator
 
Join Date: Sep 2003
Location: NJ
Posts: 19,624
Quote:
Originally Posted by jaminben View Post
I'll poke my nose in here and go with Plucky on this one as I'm pretty sure this is how Sage do it themselves when searching for or linking to/from menu items.... e.g when returning to a menu after playback has finished from within the MediaPlayer OSD.

Of course take what I say with a pinch of salt as Greg (and Plucky) have light years more expirence than myself
No, this isn't really how the developers at SageTV do it. As one of the developers, I'll go with Greg's suggestion. Just because Studio doesn't have a way to set up well defined functions, doesn't mean you can't & shouldn't approximate it when using shared code. And, before someone points out a bad example in the default STV - I don't claim coding perfection, but there are plenty of cases where I use a REM statement as the entry point to shared code with the variables to be used named & explained.

Returning to a menu after playback ends really doesn't have much to do with this discussion. It returns to the previous menu based on what the menu history contains and whether that matches a global variable that was set when playback started. While it does check which menu to return to, it does not do so by hard-coding any menu IDs when playback starts nor when it ends and then comparing them. The only hard coded menu jumps after playback ends is when the previous menu isn't where playback started, in which case it decides what to do based on the media type, not the ID of some menu.

When I have shared code that is to perform differently based on some sort of context, I do not code in a set of conditionals/branches that check what menu you are on (at least I don't remember ever doing so); instead, I use variable(s) to tag what different behavior is to be performed. If the code normally behaves one way, then the variable can assume a default value (usually none because the variable isn't defined for the original/default setting & I don't want to have to go back & add it everywhere) and only needs to be set in the places where the behavior needs to change.

I'll admit that I don't always add a comment for the behavior-changing variable, but I should, if for no other reason than it would make my life easier the next time I need to update/use that code. And, while there is no comment widget type, using an action widget with a string that starts "REM <insert descriptive comment here>" is the generally recognized way to add a comment & is what the Studio manual suggests.


Go with the better programming style -- it may take longer when first writing the code, but it simplifies reuse and maintenance of the code later.

- Andy
__________________
SageTV Open Source v9 is available.
- Read the SageTV FAQ. Older PDF User's Guides mostly still apply: SageTV V7.0 & SageTV Studio v7.1.
- Hauppauge remote help: 1) Basics/Extending it 2) Replace it 3) Use it w/o needing focus
- HD Extenders: A) FAQs B) URC MX-700 remote setup
Note: This is a users' forum; see the Rules. For official tech support fill out a Support Request.
Reply With Quote
  #13  
Old 12-03-2010, 07:02 AM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
Quote:
Originally Posted by Opus4 View Post
And, while there is no comment widget type,
Use some of that pull and get use one pretty please To me using "REM yadada" in a action widget just seems off. If we had a widget type of comments it would be much cleaner and probably get better coding out of all us for that matter. To me the REM method just seems messy and hard to follow. If we had a comment widget it would be easy to find them and locate comments.

Also while we on the subject a way to comment code out would be nice as well I know you could just put a false evaluation or leave it yellow as not part of the chain just thought I would through that at there as well

cheers
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Check this out panteragstk General Discussion 0 12-30-2009 12:39 PM
Code Check (Eyes Bleeding) Thraxius SageTV Studio 13 03-03-2008 01:22 PM
Menu templates: an idea for organizing STV code GKusnick SageTV Studio 0 01-27-2006 01:55 PM
Where can I find the menu title theme (Main Menu, Program Guide, Setup Menu, etc) mkanet SageTV Studio 5 11-11-2005 04:55 AM
Check out I been going SHS SageTV Software 9 05-17-2003 03:52 PM


All times are GMT -6. The time now is 12:37 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, vBulletin Solutions Inc.
Copyright 2003-2005 SageTV, LLC. All rights reserved.