Windows Calculator

repository·main·Indexed 12 days ago

https://github.com/microsoft/calculator

An open-source C++ and C# application for the Universal Windows Platform (UWP) providing standard, scientific, and programmer calculator functionality, as well as unit and currency converters. It utilizes the Model-View-ViewModel (MVVM) design pattern and a core mathematical engine called RatPack for infinite precision arithmetic.

Tokens
8.8K
Snippets
13
Records
48
Agent score
98%

What's inside Windows Calculator

  1. Understand the Windows Calculator Architecture

    main

    Windows Calculator is a C++/CX application built for the Universal Windows Platform (UWP). It follows the Model-View-ViewModel (MVVM) design pattern. The architecture is divided into three primary layers:

    1. View: Contains XAML files and custom controls for the UI (located in the Calculator project).
    2. ViewModel: Acts as an intermediary between the UI and business logic, providing data for the View to bind against (located in the CalcViewModel project).
    3. Model: Contains the core business logic and data (details in subsequent segments).

    Understanding this separation is key to knowing where to make UI changes (View), where to manage UI state and data exposure (ViewModel), and where to implement core calculation logic (Model).

  2. The New Feature Lifecycle

    main

    The development of a new feature follows four distinct stages:

    1. Feature Pitch: Submitted via GitHub issues. Pitches are reviewed against the Calculator roadmap.
    2. Planning: The goal is to produce a specification in the Calculator Spec repo. This phase may include design renderings and code prototypes.
    3. Implementation: The feature is coded by the submitter, a Microsoft team member, or community members. Once ready, a pull request is submitted.
    4. Final Product Review: The product team performs a final review to ensure readiness for release to Windows customers.
  3. Identify the core mathematical engine (RatPack)

    main

    The RatPack (Rational Pack) is the foundational component of the Calculator model. It is responsible for all mathematical computations. Unlike standard calculators that use floating-point arithmetic, the RatPack uses infinite precision arithmetic to ensure accuracy.

    Developers looking to implement or modify core mathematical logic should interface with ratpak.h.

  4. Understand the Calculator Model architecture

    main

    The Calculator's model is organized into three hierarchical layers within the CalculatorManager project. This structure separates high-level application state from low-level mathematical computation:

    1. CalculatorManager: The top layer. It manages overall application data, such as History and Memory lists, and maintains instances of various calculator engines for different modes.
    2. CalcEngine: The middle layer. It interprets and executes commands, maintains the current state of a specific calculation, and delegates mathematical operations to the RatPack.
    3. RatPack (Rational Pack): The core layer. It performs the actual mathematical operations using infinite precision arithmetic rather than standard floating-point arithmetic.

    To interact with these layers, refer to the following header files:

    • CalculatorManager.h for application-level management.
    • CalcEngine.h for command interpretation and state.
    • ratpak.h for core mathematical logic.
  5. How Windows Calculator is distributed and updated

    main

    Windows Calculator is distributed in two primary ways:

    1. Provisioned Windows App: It is included in every Windows 10 release as a provisioned app.
    2. Microsoft Store: Updates are delivered via the Microsoft Store, typically on a monthly cadence.
  6. Manage UI Layout with VisualStates

    main

    Calculator uses VisualStates to create an adaptive and responsive UI. Transitions between states are typically triggered by window resizing or specific application conditions.

    Key examples of VisualStates usage in Calculator include:

    • History/Memory Dock Panel: Transitions from a flyout (small windows) to a docked panel (large windows) in Standard, Scientific, and Programmer modes.
    • Scientific Mode Buttons: Hides certain function buttons and shows a Shift (↑) button in small windows, then rearranges to show all buttons in large windows.
    • Unit Converter Aspect Ratio: Re-arranges converter inputs and the numberpad based on Portrait or Landscape aspect ratios.

    When contributing UI changes, you must consider how your changes affect these various defined VisualStates and layouts.

  7. Verify Memory Functions in Calculators

    main

    Memory functions allow you to store and manipulate values. Note that Memory Recall (MR) is not available in Programmer mode.

    1. Store: Perform a calculation and press MS (or select M from the dropdown if in small scale).
    2. Add to Memory: Click M+ to add the current calculation to the stored value.
    3. Subtract from Memory: Click M- to subtract the current calculation from the stored value.
    4. Recall: Click MR to make the stored value the primary value.
    5. Clear: Click MC to clear all stored memory information.
  8. Understand Graphing Mode implementation and limitations

    main

    Graphing functionality is currently on the roadmap. While the UI code is present in this repository, the proprietary Microsoft graphing engine is not included in this open-source version.

    Developer builds use a mock implementation of the engine located in /src/GraphingImpl/Mocks, which is built on top of a common graphing API found in /src/GraphingInterfaces. Consequently, graphing functionality will not work in standard developer builds.

  9. Understand Currency Converter behavior in developer builds

    main
    The currency converter in developer builds uses mock data instead of live data from Bing. This mock data is static and uses planet names instead of countries to ensure it is clearly identifiable as non-production data. The production data used in the retail version of the application is not licensed for use in this repository.
  10. Verify Always-on-Top mode behavior

    main

    The Always-on-Top feature allows the calculator to remain visible above other applications. Its behavior varies depending on the calculator mode and window state.

    UI and Visibility

    • Standard Mode: The Always-on-Top button is visible. Its tooltip toggles between Keep on top and Back to full view.
    • Scientific Mode: The Always-on-Top button is hidden.
    • Compact View: When activated, the application title, hamburger menu, calculator type title, calculation expression, history button, and memory buttons are hidden to save space.

    Functional Constraints in Always-on-Top

    • Keyboard Shortcuts: Certain shortcuts like Ctrl-H (History) and memory shortcuts are disabled while in Always-on-Top mode.
    • Error Handling: If an undefined operation occurs (e.g., division by zero / 0), the display shows Result is undefined.
    • Button Availability: When exiting Always-on-Top mode from an error state, most operator and memory buttons may be disabled until the error is cleared.

    Window Resizing and Persistence

    • Responsive Layout: Buttons automatically expand or shrink to fit the window size. In very small vertical windows, specific buttons (like percent, square-root, squared, and reciprocal) may disappear.
    • State Persistence: The application attempts to restore the window size and Always-on-Top state from the previous session upon relaunch.
  11. Set up the development environment for Windows Calculator

    main

    To build and run the Windows Calculator app from source, ensure your environment meets the following requirements:

    Prerequisites

    • OS: Windows 11, build 22000 or newer.
    • Visual Studio: Install the latest version (Community Edition is sufficient) with the following components:
      • "Universal Windows Platform Development" workload.
      • "C++ Universal Windows Platform tools" (optional component).
      • Latest Windows 11 SDK.
    • Extensions: Install the XAML Styler Visual Studio extension.
    • UI Testing: Install Windows Application Driver (WinAppDriver) to run UI tests.

    Installation Steps

    1. Clone the repository:
      git clone https://github.com/Microsoft/calculator.git
    2. Open src\Calculator.slnx in Visual Studio to build and run the application.
    git clone https://github.com/Microsoft/calculator.git