|
21 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22 | 22 | * SOFTWARE.
|
23 | 23 | */
|
| 24 | +package com.mallowigi.idea.themes |
24 | 25 |
|
25 |
| -package com.mallowigi.idea.themes; |
| 26 | +import com.intellij.ide.ui.LafManager |
| 27 | +import com.intellij.openapi.actionSystem.AnActionEvent |
| 28 | +import com.intellij.openapi.project.DumbAware |
| 29 | +import com.mallowigi.idea.MaterialThemeBundle.message |
| 30 | +import org.jetbrains.annotations.NonNls |
| 31 | +import java.text.MessageFormat |
26 | 32 |
|
27 |
| -import com.intellij.ide.ui.LafManager; |
28 |
| -import com.intellij.openapi.actionSystem.AnActionEvent; |
29 |
| -import com.intellij.openapi.project.DumbAware; |
30 |
| -import com.intellij.util.containers.ContainerUtil; |
31 |
| -import com.mallowigi.idea.MaterialThemeBundle; |
32 |
| -import org.jetbrains.annotations.NonNls; |
33 |
| -import org.jetbrains.annotations.NotNull; |
34 |
| - |
35 |
| -import javax.swing.*; |
36 |
| -import java.text.MessageFormat; |
37 |
| -import java.util.Objects; |
38 |
| - |
39 |
| -/** |
40 |
| - * Abstract Material Theme switch action |
41 |
| - */ |
42 |
| -public abstract class MTAbstractThemeAction extends MTToggleAction implements DumbAware { |
43 |
| - |
44 |
| - @Override |
45 |
| - public final void setSelected(@NotNull final AnActionEvent e, final boolean state) { |
| 33 | +abstract class MTAbstractThemeAction : MTToggleAction(), DumbAware { |
| 34 | + override fun setSelected(e: AnActionEvent, state: Boolean) { |
46 | 35 | // Find LAF theme and trigger a theme change
|
47 |
| - final LafManager lafManager = LafManager.getInstance(); |
48 |
| - final UIManager.LookAndFeelInfo lafInfo = ContainerUtil.find(lafManager.getInstalledLookAndFeels(), |
49 |
| - lookAndFeelInfo -> lookAndFeelInfo.getName().equals(getThemeName(e))); |
50 |
| - |
51 |
| - if (lafInfo != null) { |
52 |
| - lafManager.setCurrentLookAndFeel(lafInfo); |
53 |
| - } |
| 36 | + val lafManager = LafManager.getInstance() |
| 37 | + val lafInfo = lafManager.installedLookAndFeels.find { it.name == getThemeName(e) } |
| 38 | + if (lafInfo != null) lafManager.currentLookAndFeel = lafInfo |
54 | 39 | }
|
55 | 40 |
|
56 |
| - @Override |
57 |
| - public final boolean isSelected(@NotNull final AnActionEvent e) { |
58 |
| - return Objects.requireNonNull(LafManager.getInstance().getCurrentLookAndFeel()).getName().equals(getThemeName(e)); |
59 |
| - } |
| 41 | + override fun isSelected(e: AnActionEvent): Boolean = LafManager.getInstance().currentLookAndFeel!!.name == getThemeName(e) |
60 | 42 |
|
61 |
| - /** |
62 |
| - * Returns the theme to apply |
63 |
| - * |
64 |
| - * @return the theme |
65 |
| - */ |
66 |
| - protected abstract MTThemes getTheme(); |
| 43 | + protected abstract val theme: MTThemes |
67 | 44 |
|
68 | 45 | @NonNls
|
69 |
| - private String getThemeName(@NotNull final AnActionEvent e) { |
70 |
| - final String contrast = MaterialThemeBundle.message("contrast"); |
71 |
| - |
72 |
| - final boolean isContrast = e.getPresentation().getText() != null && e.getPresentation().getText().contains(contrast); |
73 |
| - final String name = getTheme().getName(); |
74 |
| - |
75 |
| - return isContrast ? MessageFormat.format("{0} {1}", name, contrast) : name; |
| 46 | + private fun getThemeName(e: AnActionEvent): String { |
| 47 | + val contrast = message("contrast") |
| 48 | + val isContrast = e.presentation.text!!.contains(contrast) |
| 49 | + val name = theme.themeName |
| 50 | + return if (isContrast) MessageFormat.format("{0} {1}", name, contrast) else name |
76 | 51 | }
|
77 | 52 | }
|
0 commit comments