Search in sources :

Example 1 with ConfigurationListener

use of com.android.tools.idea.configurations.ConfigurationListener in project android by JetBrains.

the class Scene method add.

/**
   * Add the NlComponents contained in the given NlModel to the Scene
   *
   * @param model the NlModel to use
   */
public void add(@NotNull NlModel model) {
    ConfigurationListener listener = (flags) -> {
        if ((flags & CFG_DEVICE) != 0 && !isManualDpiFactor) {
            float newDpiFactor = model.getConfiguration().getDensity().getDpiValue() / 160f;
            if (myDpiFactor != newDpiFactor) {
                // Update from the model to update the dpi
                updateFrom(model);
            }
        }
        return true;
    };
    model.getConfiguration().addListener(listener);
    Disposer.register(model, () -> model.getConfiguration().removeListener(listener));
    List<NlComponent> components = model.getComponents();
    if (components.size() != 0) {
        NlComponent rootComponent = components.get(0).getRoot();
        myAnimate = false;
        myRoot = updateFromComponent(rootComponent);
        myAnimate = true;
        addTargets(myRoot);
    }
    model.addListener(this);
    myModel = model;
    // let's make sure the selection is correct
    if (myScreenView != null) {
        selectionChanged(myScreenView.getSelectionModel(), myScreenView.getSelectionModel().getSelection());
    }
}
Also used : ConfigurationListener(com.android.tools.idea.configurations.ConfigurationListener) ViewGroupHandler(com.android.tools.idea.uibuilder.api.ViewGroupHandler) InputEvent(java.awt.event.InputEvent) PropertiesComponent(com.intellij.ide.util.PropertiesComponent) java.util(java.util) SdkConstants(com.android.SdkConstants) com.android.tools.idea.uibuilder.scene.target(com.android.tools.idea.uibuilder.scene.target) ConfigurationListener(com.android.tools.idea.configurations.ConfigurationListener) java.awt(java.awt) VisibleForTesting(com.android.annotations.VisibleForTesting) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) com.android.tools.idea.uibuilder.model(com.android.tools.idea.uibuilder.model) ScreenView(com.android.tools.idea.uibuilder.surface.ScreenView) ConstraintLayoutHandler(com.android.tools.idea.uibuilder.handlers.constraint.ConstraintLayoutHandler) Disposer(com.intellij.openapi.util.Disposer) ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler) ApplicationManager(com.intellij.openapi.application.ApplicationManager) DisplayList(com.android.tools.idea.uibuilder.scene.draw.DisplayList) NotNull(org.jetbrains.annotations.NotNull) CFG_DEVICE(com.android.tools.idea.configurations.ConfigurationListener.CFG_DEVICE)

Example 2 with ConfigurationListener

use of com.android.tools.idea.configurations.ConfigurationListener in project android by JetBrains.

the class Configuration method updated.

/** Called when one or more attributes of the configuration has changed */
public void updated(int flags) {
    myNotifyDirty |= flags;
    myFolderConfigDirty |= flags;
    myModificationCount++;
    if (myManager.getStateVersion() != myProjectStateVersion) {
        myNotifyDirty |= MASK_PROJECT_STATE;
        myFolderConfigDirty |= MASK_PROJECT_STATE;
        myDevice = null;
        myState = null;
    }
    if (myBulkEditingCount == 0) {
        int changed = myNotifyDirty;
        if (myListeners != null) {
            for (ConfigurationListener listener : myListeners) {
                listener.changed(changed);
            }
        }
        myNotifyDirty = 0;
    }
}
Also used : ConfigurationListener(com.android.tools.idea.configurations.ConfigurationListener)

Example 3 with ConfigurationListener

use of com.android.tools.idea.configurations.ConfigurationListener in project android by JetBrains.

the class ConfigurationTest method testListener.

public void testListener() throws Exception {
    final AndroidFacet facet = AndroidFacet.getInstance(myModule);
    assertNotNull(facet);
    ConfigurationManager manager = facet.getConfigurationManager();
    assertNotNull(manager);
    assertSame(manager, facet.getConfigurationManager());
    Configuration configuration = Configuration.create(manager, null, new FolderConfiguration());
    assertNotNull(configuration);
    ConfigurationListener listener = new ConfigurationListener() {

        @Override
        public boolean changed(int flags) {
            myFlags = flags;
            myChangeCount++;
            return true;
        }
    };
    configuration.addListener(listener);
    myChangeCount = 0;
    myFlags = 0;
    configuration.setTheme("@style/MyTheme");
    assertEquals(1, myChangeCount);
    assertEquals(CFG_THEME, myFlags);
    // No firing when no change:
    configuration.setTheme("@style/MyTheme");
    assertEquals(1, myChangeCount);
    assertEquals(CFG_THEME, myFlags);
    myChangeCount = 0;
    myFlags = 0;
    configuration.startBulkEditing();
    configuration.setTheme("@style/MyTheme2");
    assertEquals(0, myChangeCount);
    assertEquals(0, myFlags);
    configuration.setActivity("foo.bar.MyActivity");
    assertEquals(0, myChangeCount);
    assertEquals(0, myFlags);
    configuration.finishBulkEditing();
    assertEquals(1, myChangeCount);
    assertEquals(CFG_THEME | CFG_ACTIVITY, myFlags);
    myChangeCount = 0;
    myFlags = 0;
    configuration.startBulkEditing();
    configuration.setTheme("@style/MyTheme3");
    configuration.setActivity("foo.bar.MyActivity3");
    configuration.setNightMode(NightMode.NIGHT);
    configuration.finishBulkEditing();
    assertEquals(1, myChangeCount);
    assertEquals(CFG_THEME | CFG_ACTIVITY | CFG_NIGHT_MODE, myFlags);
}
Also used : ConfigurationListener(com.android.tools.idea.configurations.ConfigurationListener) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 4 with ConfigurationListener

use of com.android.tools.idea.configurations.ConfigurationListener in project android by JetBrains.

the class NestedConfigurationTest method testListener.

public void testListener() throws Exception {
    final AndroidFacet facet = AndroidFacet.getInstance(myModule);
    assertNotNull(facet);
    ConfigurationManager manager = facet.getConfigurationManager();
    assertNotNull(manager);
    assertSame(manager, facet.getConfigurationManager());
    Configuration parent = Configuration.create(manager, null, new FolderConfiguration());
    assertNotNull(parent);
    NestedConfiguration configuration = NestedConfiguration.create(parent);
    assertNotNull(configuration);
    ConfigurationListener listener = new ConfigurationListener() {

        @Override
        public boolean changed(int flags) {
            myFlags = flags;
            myChangeCount++;
            return true;
        }
    };
    configuration.addListener(listener);
    myChangeCount = 0;
    myFlags = 0;
    configuration.setOverrideLocale(true);
    configuration.setLocale(Locale.create("en-rUS"));
    assertEquals(1, myChangeCount);
    assertEquals(CFG_LOCALE, myFlags);
    // No firing when only seeing overridden changes
    myFlags = 0;
    parent.setLocale(Locale.create("es"));
    assertEquals(1, myChangeCount);
    assertEquals(0, myFlags);
    myFlags = 0;
    parent.setLocale(Locale.create("de"));
    parent.setUiMode(UiMode.APPLIANCE);
    assertEquals(2, myChangeCount);
    assertEquals(CFG_UI_MODE, myFlags);
}
Also used : ConfigurationListener(com.android.tools.idea.configurations.ConfigurationListener) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Aggregations

ConfigurationListener (com.android.tools.idea.configurations.ConfigurationListener)4 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)2 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)2 SdkConstants (com.android.SdkConstants)1 VisibleForTesting (com.android.annotations.VisibleForTesting)1 CFG_DEVICE (com.android.tools.idea.configurations.ConfigurationListener.CFG_DEVICE)1 ViewGroupHandler (com.android.tools.idea.uibuilder.api.ViewGroupHandler)1 ViewHandler (com.android.tools.idea.uibuilder.api.ViewHandler)1 ConstraintLayoutHandler (com.android.tools.idea.uibuilder.handlers.constraint.ConstraintLayoutHandler)1 com.android.tools.idea.uibuilder.model (com.android.tools.idea.uibuilder.model)1 DisplayList (com.android.tools.idea.uibuilder.scene.draw.DisplayList)1 com.android.tools.idea.uibuilder.scene.target (com.android.tools.idea.uibuilder.scene.target)1 ScreenView (com.android.tools.idea.uibuilder.surface.ScreenView)1 PropertiesComponent (com.intellij.ide.util.PropertiesComponent)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 Disposer (com.intellij.openapi.util.Disposer)1 java.awt (java.awt)1 InputEvent (java.awt.event.InputEvent)1 java.util (java.util)1 List (java.util.List)1