001package polaris.sdk.ui.samples; 002 003import polaris.sdk.ui.*; 004import polaris.sdk.ui.controls.*; 005import static polaris.sdk.ui.samples.SceneSupport.*; 006 007/** Six stateful, reusable application scenes made with existing LiquidGlass UI controls. */ 008public final class SceneExamples extends Panel { 009 public static final String[] IDS={"food","chat","client","music","settings","files"}; 010 public static final String[] TITLES={"外卖点餐","即时聊天","外挂面板","音乐播放","系统设置","文件管理"}; 011 private final ButtonGroup selector=new ButtonGroup(TITLES); 012 private final Label note=caption("同一套控件,不同的生活场景。各示例数据仅保存在当前窗口。"); 013 private final SceneSupport.AppScene[] scenes=new SceneSupport.AppScene[IDS.length]; 014 private SceneSupport.AppScene active;private int selected; 015 public SceneExamples(){glass(false);padding(0);gap(Spacing.GAP);clipChildren(false);id("sceneExamples"); 016 mount(this,selector,"scene.selector");mount(this,note,"scene.note"); 017 for(int i=0;i<IDS.length;i++)selector.children().get(i).id("scene.tab."+IDS[i]); 018 selector.onChange(v->select(selector.selectedIndex()));select(0); 019 } 020 public int selectedIndex(){return selected;} 021 public Widget selectedScene(){return active;} 022 public Widget scene(int index){if(index<0||index>=IDS.length)throw new IllegalArgumentException("scene index");if(scenes[index]==null){switch(index){case 0:scenes[index]=new FoodScene();break;case 1:scenes[index]=new ChatScene();break;case 2:scenes[index]=new ClientScene();break;case 3:scenes[index]=new MusicScene();break;case 4:scenes[index]=new SettingsScene();break;case 5:scenes[index]=new FilesScene();break;default:throw new AssertionError();}}return scenes[index];} 023 public void select(int index){if(index<0||index>=IDS.length)throw new IllegalArgumentException("scene index"); 024 if(active!=null&&selected==index){if(selector.selectedIndex()!=index)selector.select(index);return;} 025 if(active!=null)remove(active);selected=index;active=(SceneSupport.AppScene)scene(index);add(active);active.appear(); 026 if(selector.selectedIndex()!=index)selector.select(index);changed(); 027 } 028 public static int indexOf(String id){for(int i=0;i<IDS.length;i++)if(IDS[i].equalsIgnoreCase(id))return i;try{int i=Integer.parseInt(id);if(i>=0&&i<IDS.length)return i;}catch(NumberFormatException ignored){}throw new IllegalArgumentException("Scene must be food, chat, client, music, settings, files or 0..5");} 029 @Override protected void layoutChildren(){ 030 place(selector,0,0,w,44);place(note,0,58,w,22);float needed=active.preferredHeight(w);place(active,0,98,w,needed);h=98+needed; 031 } 032}